Transcript
Page 1: Go programming introduction

GO PROGRAMMING

GINTO JOSEPH1325919COMPUTER SCIENCE DEPTCHRIST UNIVERSITY BANGLORE

Page 2: Go programming introduction
Page 3: Go programming introduction
Page 4: Go programming introduction

AGENDA * Introduction

* History* Versions of go* Key Words* Operators* Programs* Conclusion* References

Page 5: Go programming introduction

• Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

• Go language is for google language• Developed from c & c++• General purpose language• Strongly typed language• Automatic garbage collection• Programs constructed from the packages

Introduction

Page 6: Go programming introduction

History

• Introduced in 2007• Language announced in November 2009• Developed from C and C++ language • GC compiler is used to compile the program• BSD(Berkeley Software Distribution) Licensed

Page 7: Go programming introduction

Creator of Go programming is Robert Griesemer, Robe Pike, And Ken Thompson

ROBERT ROBE PICK KEN THOMPSON

Page 9: Go programming introduction

Key Words of go

• Break , case , chan , const , continue , default , defer , else , fallthrough , for , func , go , goto, if , import , interface , map , package , range , return , select , struct , switch , type , var

Page 10: Go programming introduction

Operators

Operators combine operands into expressions.

Types of Operators1. Arithmetic Operators2. Comparison Operators3. Integer Overflow4.Logical Operators5.Address operators6.Receive operators

Page 11: Go programming introduction

• Arithmetic OperatorsArithmetic operators apply to numeric values and yield a result of the same

type as the first operand. The four standard arithmetic operators (+, -, *, /) apply to integer, floating-point, and complex

types; + also applies to strings. All other arithmetic operators apply to integers only.

Page 12: Go programming introduction

• + sum integers, floats, complex values, strings• - difference integers, floats, complex values • * product integers, floats, complex values • / quotient integers, floats, complex values • % remainder integers • & bitwise AND integers • | bitwise OR integers • ^ bitwise XOR integers • &^ bit clear (AND NOT) integers • << left shift integer • << unsigned integer • >> right shift integer • >> unsigned integer

Page 13: Go programming introduction

• Strings can be concatenated using the + operator or the += assignment operator:

• s := "hi" • s += " and good bye“

Page 14: Go programming introduction

Comparison Operators in go == equal != not equal < less <= less or equal > greater >= greater or equal

Logical operators in go && conditional AND

|| conditional OR ! NOT

Page 15: Go programming introduction

Object Oriented GO

No classes , no InheritanceGo support encapsulation

Page 16: Go programming introduction

Comparison between c++, java, javascript and Go

Page 17: Go programming introduction
Page 18: Go programming introduction
Page 19: Go programming introduction
Page 20: Go programming introduction

Go by Example: Hello World

package main import "fmt" func main() { fmt.Println("hello world") }

Page 21: Go programming introduction

Go by Example: ValuesGo has various value types including strings, integers,

floats, booleans, etc.eg: package main

import "fmt" func main() {fmt.Println("go" + "lang")fmt.Println("1+1 =", 1+1)fmt.Println("7.0/3.0 =", 7.0/3.0)fmt.Println(true && false)fmt.Println(true || false)fmt.Println(!true)}

Page 22: Go programming introduction

go by Example: Variablespackage main import "fmt" func main() {var a string = "initial" fmt.Println(a)var b, c int = 1, 2 fmt.Println(b, c)var e int fmt.Println(e)}

Page 23: Go programming introduction

Go by Example: Forfor is Go’s only looping construct. Here are three basic types of for loops.

package main import "fmt" func main() { i := 1 for i <= 3 { fmt.Println(i) i = i + 1 } for j := 7; j <= 9; j++ { fmt.Println(j) } for { fmt.Println("loop") break } }

Page 24: Go programming introduction

Go by Example: If/ElseBranching with if and else in Go is straight-forward.You can have an if statement without an else.

package main import "fmt“func main() {if 7%2 == 0 {fmt.Println("7 is even")} else {fmt.Println("7 is odd")}}

Page 25: Go programming introduction

Go by Example: SwitchSwitch statements express conditionals across many branches.

package mainimport "fmt“func main() {i := 2switch i { case 1: fmt.Println("one") case 2: fmt.Println("two") case 3: fmt.Println("three") }}

Page 26: Go programming introduction

Go by Example: ArraysIn Go, an array is a numbered sequence of elements of a specific length.package main import "fmt" func main() {var a [5]int fmt.Println("emp:", a)b := [5]int{1, 2, 3, 4, 5} fmt.Println("dcl:", b)var twoD [2][3]int

for i := 0; i < 2; i++ { for j := 0; j < 3; j++ {

twoD[i][j] = i + j}

} fmt.Println("2d: ", twoD) }

Page 27: Go programming introduction

Go by Example: SlicesA slice is a segment of an arrayArrays slices are indexable and have a length

func main() {slice1 := []int{1,2,3}slice2 := append(slice1, 4, 5)fmt.Println(slice1, slice2)}

Page 28: Go programming introduction

Go by Example: FunctionsFunctions are central in Go. We’ll learn about functions with a few different examples. A function is an independent section of code that maps zero or more input parameters to zero or more output parameters

package main import "fmt“func plus(a int, b int) int {return a + b}func main() {res := plus(1, 2)fmt.Println("1+2 =", res)}

Page 29: Go programming introduction

Go by Example: Multiple Return ValuesGo has built-in support for multiple return values. This feature is used often in idiomatic Go, for example to return both result and error values from a function

package main import "fmt“func vals() (int, int) { return 3, 7} func main() {a, b := vals() fmt.Println(a) fmt.Println(b)_, c := vals() fmt.Println(c)}

Page 30: Go programming introduction

Go by Example: PointersGo supports pointers, allowing you to pass references to values and records within your program.

package main import "fmt"func zeroval(ival int) { ival = 0}func zeroptr(iptr *int) { *iptr = 0}func main() { i := 1 fmt.Println("initial:", i) zeroval(i) fmt.Println("zeroval:", i)zeroptr(&i) fmt.Println("zeroptr:", i)fmt.Println("pointer:", &i) }

Page 31: Go programming introduction

Conclusion

• Go is a fast, Small, easy to learn language• Actively developed by google• Tones of third party libraries• Learn it by using it

Page 32: Go programming introduction

REFERENCES• [1] A. Ashoroft and A. Manna, 'The translation of 'go to' programs

to 'while' programs," in Proc. Int. Fed. Inform. Processing Congr., 1971, vol. 1. Amsterdam, The Netherlands: North-Holland, 1972.

• [2] S. L. Bloom and C. C. Elgot, "The existence and construction offree iterative theories," IBM Corp., Yorktown Heights, NY, Res.Rep. RC-4937, July 1974;also to appear in . Comput. Syst. Sci.

• [3] A. Bohm and A. Jacopini, "Flow diagrams, turing machines andlanguages with only two formation rules," Commun. Ass. Comput.Mach., May 1966.

• [4] A. Bruno and A. Stieglitz, "The expression of algorithms bycharts," J. Ass. Comput. Mach., July 1972.

• [5] C. C. Elgot, 'The common algebraic structure of exit automataand machines," Comput., Jan. 1971.

Page 33: Go programming introduction
Page 34: Go programming introduction

Top Related