其他分享
首页 > 其他分享> > [GO] Pass by reference

[GO] Pass by reference

作者:互联网

func changeName(name *string) {
	*name = strings.ToUpper(*name)
}

// Coordinates
type Coordinates struct {
	X, Y float64
}

func main() {
	name := "Elvis"
	changeName(&name)
	fmt.Println(name) // ELVIS

	var c = Coordinates{X: 10, Y: 20}
	// If pass c by value, then it won't modify c
	coordAddress := c
	coordAddress.X = 200
	fmt.Println(coordAddress) // {200 20}
	fmt.Println(c)            // {10 20}

	// If pass c by refernece, then it will modify c as well
	coordAddress2 := &c
	coordAddress2.X = 200
	fmt.Println(*coordAddress2) // {200 20}
	fmt.Println(c)              // {200 20}
}

 

标签:200,20,name,reference,fmt,Coordinates,Println,Pass,GO
来源: https://www.cnblogs.com/Answer1215/p/16660294.html