我对Go编程语言相当不熟悉,我一直在尝试找到一种方法来获取字符串形式的变量类型。到目前为止,我还没有找到任何可以工作的东西。我曾尝试使用typeof(variableName)来获取字符串形式的变量类型,但这似乎无效。
Go有没有内置的运算符可以获得字符串形式的变量类型,类似于JavaScript的typeof运算符或Python的type运算符?
//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.
}发布于 2013-07-22 10:42:20
如果你只想打印文字,那么:fmt.Printf("%T", num)就行了。http://play.golang.org/p/vRC2aahE2m
https://stackoverflow.com/questions/17779136
复制相似问题