go是有指针的

  1. 概念

与C/C++类似, 没有指针运算(unsafe除外)

  1. 参数传递

    go中只有值传递 ,没有引用传递的机制,但是可以用指针

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    func swap(a, b *int){
    *a,*b = *b,*a
    }

    func main() {
    a,b :=1,2
    fmt.Println(a,b)
    swap(&a,&b)
    fmt.Println(a,b)
    }
    //or 这种更优雅
    func swap(a, b int)(int, int){
    return b, a
    }