How do I dereference a pointer in Go?

huangapple go评论94阅读模式
英文:

How do I dereference a pointer in Go?

问题

在Go语言中,有没有办法将*string转换为string?(或者,同样地,将任何*T转换为T?)

我在互联网上搜索了一下,并查阅了一些Go文档,但是我找不到相关信息,可能是我漏掉了。

英文:

In the Go language, is there any way to convert *string to string? (or, for that matter, any *T to T?)

I have looked on the internet and through some Go documentation, but I can't find it - may have missed it.

答案1

得分: 7

*T转换为T,可以使用*运算符:

func Dereference(strptr *string) string {
    return *strptr
}

在继续学习这门语言之前,我强烈建议你先了解一下指针。它们是一种基本概念,没有它们的话,就无法高效地使用这门语言。

英文:

To turn a *T into a T, use the * operator:

func Dereference(strptr *string) string {
    return *strptr
}

I highly suggest you to read about pointers before proceeding with the language. They are a fundamental concept without which it is impossible to use the language efficiently.

huangapple
  • 本文由 发表于 2014年4月27日 06:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/23317045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定