[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}
}

 

posted @   Zhentiw  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-09-06 [Javascript] Memoization
2019-09-06 [AngularJS] Extend Controller
2018-09-06 [TypeScript] Understanding Decorators
2017-09-06 [TypeScript@2.5] Omit catch error block if not needed
2017-09-06 [React] Configure a React & Redux Application For Production Deployment and Deploy to Now
2016-09-06 [Javascript] Functor Basic Intro
2015-09-06 [React + webpack] hjs-webpack
点击右上角即可分享
微信分享提示