在Go编程语言中,是否可以将变量的类型作为字符串获取?

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

In the Go programming language, is it possible to obtain a variable's type as a string?

问题

我对Go编程语言不太熟悉,一直在尝试找到一种将变量的类型作为字符串获取的方法。到目前为止,我还没有找到有效的方法。我尝试使用typeof(variableName)来获取变量的类型作为字符串,但这似乎是无效的。

Go是否有任何内置运算符可以获取变量的类型作为字符串,类似于JavaScript的typeof运算符或Python的type运算符?

  1. //尝试将变量的类型作为字符串打印出来:
  2. package main
  3. import "fmt"
  4. func main() {
  5. num := 3
  6. fmt.Println(typeof(num))
  7. //我期望这会打印出"int",但是typeof似乎是一个无效的函数名。
  8. }
英文:

I'm fairly unfamiliar with the Go programming language, and I've been trying to find a way to get the type of a variable as a string. So far, I haven't found anything that works. I've tried using typeof(variableName) to obtain a variable's type as a string, but this doesn't appear to be valid.

Does Go have any built-in operator that can obtain a variable's type as a string, similar to JavaScript's typeof operator or Python's type operator?

  1. //Trying to print a variable's type as a string:
  2. package main
  3. import "fmt"
  4. func main() {
  5. num := 3
  6. fmt.Println(typeof(num))
  7. //I expected this to print "int", but typeof appears to be an invalid function name.
  8. }

答案1

得分: 14

如果你只想打印类型,那么 fmt.Printf("%T", num) 就可以工作。

英文:

If you just want to print the type then: fmt.Printf("%T", num) will work. http://play.golang.org/p/vRC2aahE2m

答案2

得分: 13

reflect包中有一个TypeOf函数:

  1. package main
  2. import "fmt"
  3. import "reflect"
  4. func main() {
  5. num := 3
  6. fmt.Println(reflect.TypeOf(num))
  7. }

这将输出:

  1. int

更新: 你更新了你的问题,指定你想要将类型作为字符串返回。TypeOf返回一个Type,它有一个Name方法,可以将类型作为字符串返回。所以:

  1. typeStr := reflect.TypeOf(num).Name()

更新2: 为了更全面,我应该指出你可以选择在Type上调用Name()String()方法;它们有时是不同的:

  1. // Name返回类型在其包中的名称。
  2. // 对于无名称的类型,它返回一个空字符串。
  3. Name() string

与:

  1. // String返回类型的字符串表示。
  2. // 字符串表示可能使用缩写的包名
  3. // (例如,base64代替"encoding/base64"),
  4. // 并且不能保证在类型之间是唯一的。要进行相等性测试,
  5. // 直接比较Types。
  6. String() string
英文:

There's the TypeOf function in the reflect package:

  1. package main
  2. import "fmt"
  3. import "reflect"
  4. func main() {
  5. num := 3
  6. fmt.Println(reflect.TypeOf(num))
  7. }

This outputs:

<pre>
int
</pre>

Update: You updated your question specifying that you want the type as a string. TypeOf returns a Type, which has a Name method that returns the type as a string. So

  1. typeStr := reflect.TypeOf(num).Name()

Update 2: To be more thorough, I should point out that you have a choice between calling Name() or String() on your Type; they're sometimes different:

  1. // Name returns the type&#39;s name within its package.
  2. // It returns an empty string for unnamed types.
  3. Name() string

versus:

  1. // String returns a string representation of the type.
  2. // The string representation may use shortened package names
  3. // (e.g., base64 instead of &quot;encoding/base64&quot;) and is not
  4. // guaranteed to be unique among types. To test for equality,
  5. // compare the Types directly.
  6. String() string

huangapple
  • 本文由 发表于 2013年7月22日 10:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/17779136.html
匿名

发表评论

匿名网友

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

确定