Contents 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
package Only package main is executable Must be the same in directory Default name when imported Convention: same with the last element of the import path
If we do not follow the convention, foo/greeting.go 1 package bar // <-- import path is foo 2 import "fmt" 3 func Greeting () { 4 fmt.Println("Hello ,␣World") 5 }
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Short Assignment Inside function, := short assignment can be used in place of var with implicit type. 1 func main() { 2 a, b, c := "foo", true , 3.14 3 }
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
range 1 pow := make ([]int , 10) 2 for i := range pow { // only key 3 pow[i] = 1 << uint(i) 4 } 5 for _, value := range pow { // only value 6 fmt.Printf("%d\n", value) 7 }
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Methods Cont. In fact, you can define a method on any type you define in your package, not just structs. You cannot define a method on a type from another package, or on a basic type.
Interfaces 1 type SqrtError float64 2 func (err *SqrtError) Error () string { 3 return fmt.Sprintf("Sqrt:␣%v␣is␣nagative", float64 (*err)) 4 } 5 var err = SqrtError (3) 6 var x error = 1 // => type error 7 var y error = err // => type error 8 var z error = &err // => OK
Interfaces and Pointer 1 type SqrtError float64 2 func (err SqrtError) Error () string { 3 return fmt.Sprintf("Sqrt:␣%v␣is␣nagative", float64(err)) 4 } 5 var err = SqrtError (3) 6 var x error = 1 // => type error 7 var y error = err // => OK 8 var z error = &err // => OK
Embedding Last part of the type name is defined as implicit field name 1 func main() { 2 job := Job{Logger: log.New(os.Stderr , "Job:␣ ", log.Ldate)} 3 4 fmt.Println(job) 5 }
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading
Channels Channels are a typed conduit through which you can send and receive values with the channel operator, <-. 1 ch <- v // send v to channel 2 v := <-ch // receive from ch , and assign to v
Outline 1 How to Write Go Code 2 A Tour of Go Hello World Functions Variables Control Statements Data Structures Methods and Interfaces Concurrency 3 Continue Reading