golang 被 * & 搞晕了,求解答
时间:2023-02-16
浏览:482
作者:佚名
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《golang 被 * & 搞晕了,求解答》,介绍一下go,希望对大家的知识积累有所帮助,助力实战开发!
问题内容

如图
按我的理解
var ticker *Ticker // 是一个指针变量
ticker = &Ticker{ // 指针指向结构体的地址
Url: "werwerwer",
Value: 33333333,
}
fmt.Println(ticker.Url) // 应该输出ticker.Url 的内存地址才对啊,怎么直接是值?
没有系统语言的编程经验
对指针还不太理解
求大神解答
正确答案
你的
Ticker结构体中的
Url是
string类型,并不是
*string, 所以可以正确输出 字符信息。
至于结构体的指针,文档上描述为:
Pointers to structs
Struct fields can be accessed through a struct pointer.To access the field X of a struct when we have the struct pointer p we
could write (*p).X. However, that notation is cumbersome, so the language permits us instead to write just p.X, without the explicit dereference.
就是说结构体的的字段/方法可以通过指针直接使用,而不用啰嗦的显性写成(*p),这是语言允许的。
理论要掌握,实操不能落!以上关于《golang 被 * & 搞晕了,求解答》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
站内热门
最新文章
12.01
热门专题
