本文共 572 字,大约阅读时间需要 1 分钟。
一句话,函数传入指针参数可用语法糖省掉,但目前版本只针对结构体有效,如下*
package mainimport ( "fmt")func main() { s1 := 2 fmt.Println("p1:", p1(&s1)) //p1: 3 s2 := 2 p2(&s2) fmt.Println("p2:", s2) //p2: 3 var s3 ss1 s3.b = 2 p3(&s3) fmt.Println("p3:", s3) //p3: 4}func p1(x *int) int { fmt.Println(x) fmt.Println(*x) (*x)++ //x++ //(*x 语法糖无法使用) return *x}func p2(x *int) { fmt.Println(x) fmt.Println(*x) *x = 20 //x++ //(*x 语法糖无法使用)}func p3(x *ss1) { fmt.Println(x) fmt.Println(*x) (*x).b++ x.b++ //(*x 语法糖可用)}type ss1 struct { a string b int c []int d map[string]int}
所以建议还是用完整方式写,远离某些糖
转载地址:http://vvkpi.baihongyu.com/