golang 被 * & 搞晕了,求解答
本文介绍了golang 被 * & 搞晕了,求解答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
如图
按我的理解
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 被 * & 搞晕了,求解答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文