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

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

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运算符?

//尝试将变量的类型作为字符串打印出来:
package main

import "fmt"

func main() {
    num := 3
    fmt.Println(typeof(num))
    //我期望这会打印出"int",但是typeof似乎是一个无效的函数名。
}
英文:

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?

//Trying to print a variable's type as a string:
package main

import "fmt"

func main() {
	num := 3
	fmt.Println(typeof(num))
    //I expected this to print "int", but typeof appears to be an invalid function name.
}

答案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函数:

package main

import "fmt"
import "reflect"

func main() {
    num := 3
    fmt.Println(reflect.TypeOf(num))
}

这将输出:

int

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

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

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

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

与:

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

There's the TypeOf function in the reflect package:

package main

import "fmt"
import "reflect"

func main() {
    num := 3
    fmt.Println(reflect.TypeOf(num))
}

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

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:

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

versus:

// String returns a string representation of the type.
// The string representation may use shortened package names
// (e.g., base64 instead of &quot;encoding/base64&quot;) and is not
// guaranteed to be unique among types.  To test for equality,
// compare the Types directly.
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:

确定