gobyexample-pointers

来源:https://github.com/xg-wang/gobyexample/tree/master/examples

package main

import "fmt"

func zeroval(ival int) {
    ival = 0
}
func zeroptr(iptr *int) {
    *iptr = 0
}

func main() {
    i := 1
    fmt.Println("initial:", i)

    zeroval(i)
    fmt.Println("zeroval:", i)

    //通过`&i`语法来取得`i`的内存地址,即指向`i`的指针
    zeroptr(&i)
    fmt.Println("zeroptr:", i)

    //指针也是可以打印的
    fmt.Println("pointer:", &i)
}

输出结果:

initial: 1
zeroval: 1
zeroptr: 0
pointer: 0xc00004c080

你可能感兴趣的:(gobyexample-pointers)