func (c *App) example() // c操作对象本身
func (c App) example() // c为局部变量,对其修改不影响对象的值
前者,是修改内存中引用的值,后者是一个副本(不影响原来的内存)
package main import ( "fmt" ) type App struct{ Name string Num int } func (c *App)getName(){ c.Name = "myname" } func (c App)getNum()App{ c.Num = 11 return c } func main() { app := &App{} app.getName() app2 := app.getNum() fmt.Println(app,app2) } /* app: &{myname 0} app2: {myname 11} */
转载于:https://www.cnblogs.com/benlightning/p/4448125.html
最后
以上就是畅快冬天最近收集整理的关于golang func (c App) example() 和func (c *App) example() 区别的全部内容,更多相关golang内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复