A Tour of GO (3)--More Types

2016-01-13  本文已影响0人  陈胖子胖胖胖
  1. Pointers
    • *T is type: a pointer to T.
      var x *int
    • Operator & generate a pointer to its operand
      i :=42
      p =&i
    • The * operator denote the pointer's underlying value.
      i = 43
      fmt. Println(
      i)
  2. Struct
    type Vertex struct{
    X int
    Y int
    }
  1. Array
    The type [n]T is an array of n values of type T
  1. Map
  1. Function values
    Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables.
func adder() func(int) int {
  sum := 0
  return func(x int) int {
    sum += x  
    return sum
  }
}

func main() {
  pos, neg := adder(), adder()
  for i := 0; i < 10; i++ {
    fmt.Println(
        pos(i),
        neg(-2*i),
    )   
  }
}
上一篇 下一篇

猜你喜欢

热点阅读