Introduction to Golang
Scratching the surface
3 December 2019
Software Engineer, Heliox
Software Engineer, Heliox
The language and toolchain contains support for modern programming practices
Pros
Cons
C/C++ is just a language specification, needs many tools
Go has just a single tool
go
Go has an excellent standard library
1// All go files must start with a package name 2// the "main" package is where the main function resides 3package main 4 5// The import statement is for loading other packages 6// the "fmt" package is for formatting 7import ( 8 "fmt" 9) 10 11// The main function is the entrypoint of the executable 12func main() { 13 fmt.Println("Hello World!") 14}
Compile and run:
$ go run hello.go
1package main 2 3import "fmt" 4 5func main() { 6 // Concatenate and calculate 7 fmt.Println("foo" + "bar") 8 fmt.Println("12.3 * 2 =", 12.3*2) 9 10 // Variables always have a initialized value 11 var a uint 12 fmt.Println("a =", a) 13 14 // Multiple variables with multiple types 15 var b, c = 10.5, "chicken" 16 fmt.Printf("b(%v): %T, c(%v): %T\n", b, b, c, c) 17 18 // Single variable declare and assign 19 d := "var d" 20 fmt.Println(d) 21}16
1package main 2 3import "fmt" 4 5func main() { 6 // A classical for loop which counts from 1 to 3 7 for n := 1; n < 4; n++ { 8 fmt.Println("n =", n) 9 } 10 11 // Go doesn't have a while statement, for is used 12 sum := 1 13 for sum < 10 { 14 sum++ 15 } 16 fmt.Println("sum =", sum) 17}17
1package main 2 3import ( 4 "fmt" 5 "time" 6) 7 8func main() { 9 // Variables declared inside an if are also available in the else 10 if name, offset := time.Now().Zone(); offset == 3600 { 11 fmt.Println("We are here at UTC + 1 hour! (name: ", name, ")") 12 } else if offset == 7200 { 13 fmt.Println("We are here at UTC + 2 hour! (name: ", name, ")") 14 } else { 15 fmt.Println("We are at", name) 16 } 17}18
1package main 2 3import ( 4 "fmt" 5 "time" 6) 7 8func main() { 9 // Yes the day of the course is Tuesday 10 switch time.Tuesday { 11 // You can use commas in a case to cover multiple statements 12 case time.Tuesday, time.Friday: 13 // By default, go switch-cases don't fallthrough 14 fallthrough 15 case time.Saturday: 16 fmt.Println("Today is the big day") 17 default: 18 fmt.Println("Its a regular day") 19 } 20}19
1package main 2 3import "fmt" 4 5func main() { 6 // An array is a fixed-size object 7 wordarr := [4]byte{0xde, 0xad, 0xbe, 0xef} 8 9 // Convert the array to a slice and typecast to string 10 wordsli := wordarr[:] 11 12 // Print the length and capacity of the slice (and hex) 13 fmt.Printf("len(%d), cap(%d), %x\n", len(wordsli), cap(wordsli), string(wordsli)) 14}20
1package main 2 3import "fmt" 4 5// Create a struct to store person information 6type Person struct { 7 Name string 8 Age int 9} 10 11func main() { 12 // Create and initialize a person 13 p := Person{Name: "Jerry", Age: 30} 14 15 // Print struct including property names 16 fmt.Printf("%+v\n", p) 17}21
1package main 2 3import "fmt" 4 5type Location struct { 6 Latitude, Longitude float64 7} 8 9func main() { 10 // Allocate an empty map with make 11 m := make(map[string]Location) 12 13 // Store the location of Heliox 14 m["Heliox"] = Location{ 15 51.485945, 5.392263, 16 } 17 18 // Test and get the value of key "Heliox" 19 loc, ok := m["Heliox"] 20 fmt.Println(loc, ok) 21}22
1package main 2 3import "fmt" 4 5func main() { 6 i := 42 7 8 p := &i // point to i 9 fmt.Println(*p) // read i through the pointer 10 *p = 21 // set i through the pointer 11 fmt.Println(i) // see the new value of i 12}23
1package main 2 3import ( 4 "io" 5 "os" 6 "strings" 7) 8 9// Read data from r and write to os.Stdout 10// the io.Reader is an interface with only a Read method 11func WriteToStdout(r io.Reader) { 12 // Copy to an io.Writer (dest) from an io.Reader (source) 13 // this can be anything! File, network socket etc. 14 io.Copy(os.Stdout, r) 15} 16 17func main() { 18 // Create an io.Reader from a string and print it to os.Stdout 19 sr := strings.NewReader("Hello string reader!") 20 WriteToStdout(sr) 21}24
Playing around on play.golang.org
26