如何拥有一个类型为变量类型的变量?
我这么说是什么意思?我有 python 和 java 背景。在这两种语言中,我都可以执行诸如将类名分配给变量之类的操作。
#Python
class A:
def __init__(self):
self.a = "Some Value"
# And asigning the class name to a variable
class_A = A
# And create instance from class A by using class_A
a = class_A()
Run Code Online (Sandbox Code Playgroud)
go中有这样的机制允许我这样做吗?我不知道在他们的文档中哪里可以找到这些内容。老实说,通常我不知道这些在编程语言中叫什么。
例如我希望能够做到:
// For example, what would be the type of myType?
var myType type = int
Run Code Online (Sandbox Code Playgroud)
我将使用这种机制来获取“类型”参数。例如:
func Infer(value interface{}, type gotype) {...}
Run Code Online (Sandbox Code Playgroud)