类型、变量、函数

内建类型

  1. bool
  2. string
  3. int int8 int16 int32 int64
  4. uint uint8 uint16 uint32 uint64
  5. byte // alias for uint8
  6. rune // alias for int32
  7. float32 float64
  8. complex32 complex64

类型转换

  1. var i int = 42
  2. var f float64 = float64(i)
  3. var u uint = uint(f)

变量、常量定义

  1. var foo int // 0
  2. var foo int = 42
  3. var foo, bar int = 42, 1302 // multiple
  4. var foo = 42 // type inferred
  5. foo := 42 // shorthand, only in func bodies
  6. const bar = "This is a constant"

函数

  • 普通函数
  1. func empty() {}
  • 参数
  1. func empty(s string, c int) {}
  • 返回值
  1. func age() int {
  2. return 32
  3. }
  • 多个返回值
  1. func nameAndAge() (string, int) {
  2. return "John", 32
  3. }
  • 变参函数
  1. func sum(args ...int) int {
  2. total := 0
  3. for _, v := range args {
  4. total += v
  5. }
  6. return total
  7. }
  • 闭包
  1. func generator(step int) func() int {
  2. cur := 0
  3. return func() int {
  4. cur += step
  5. return cur
  6. }
  7. }
  • 函数作为参数
  1. func filter(f func(int) bool) bool {
  2. return f(0)
  3. }

控制流

if-else

  1. if x > 0 {
  2. x -= 1
  3. } else {
  4. x += 1
  5. }

switch

  1. switch operatingSystem {
  2. case "darwin":
  3. fmt.Println("Mac OS Hipster")
  4. // cases break automatically, no fallthrough by default
  5. case "linux":
  6. fmt.Println("Linux Geek")
  7. default:
  8. // Windows, BSD, ...
  9. fmt.Println("Other")
  10. }
  11. number := 42
  12. switch {
  13. case number < 42:
  14. fmt.Println("Smaller")
  15. case number == 42:
  16. fmt.Println("Equal")
  17. case number > 42:
  18. fmt.Println("Greater")
  19. }

for

  1. for i := 0; i < 10; i++ {
  2. }
  3. for i < 10 {
  4. }
  5. for {
  6. }

Array, Slice, Map

Array

  1. var a [10]int // declare an int array with length 10. Array length is part of the type!
  2. a[3] = 42 // set elements
  3. i := a[3] // read elements
  4. // declare and initialize
  5. var a = [2]int{1, 2}
  6. a := [2]int{1, 2} //shorthand
  7. a := [...]int{1, 2} // elipsis -> Compiler figures out array length

Slice

  1. var a []int // declare a slice - similar to an array, but length is unspecified
  2. var a = []int {1, 2, 3, 4} // declare and initialize a slice (backed by the array given implicitly)
  3. a := []int{1, 2, 3, 4} // shorthand
  4. chars := []string{0:"a", 2:"c", 1: "b"} // ["a", "b", "c"]
  5. var b = a[lo:hi] // creates a slice (view of the array) from index lo to hi-1
  6. var b = a[1:4] // slice from index 1 to 3
  7. var b = a[:3] // missing low index implies 0
  8. var b = a[3:] // missing high index implies len(a)
  9. a = append(a,17,3) // append items to slice a
  10. c := append(a,b...) // concatenate slices a and b
  11. // create a slice with make
  12. a = make([]byte, 5, 5) // first arg length, second capacity
  13. a = make([]byte, 5) // capacity is optional
  14. // create a slice from an array
  15. x := [3]string{"Лайка", "Белка", "Стрелка"}
  16. s := x[:] // a slice referencing the storage of x

Array, Slice 遍历

  1. for index, value := range a {
  2. }
  3. for _, value := range a {
  4. }
  5. for index := range a {
  6. }

Map

  1. var m map[string]int
  2. m = make(map[string]int)
  3. m["key"] = 42
  4. fmt.Println(m["key"])
  5. delete(m, "key")
  6. elem, ok := m["key"] // test if key "key" is present and retrieve it, if so
  7. // map literal
  8. var m = map[string]Vertex{
  9. "Bell Labs": {40.68433, -74.39967},
  10. "Google": {37.42202, -122.08408},
  11. }
  12. for key, value := range m {
  13. fmt.Println("Key:", key, "Value:", value)
  14. }