在Go语言中给变量分配类型。

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

Assigning types to variables in Go

问题

如何创建一个类型为变量类型的变量?

我的意思是什么?我有Python和Java的背景。在这两种语言中,我可以将类名赋值给变量。

# Python
class A:
    def __init__(self):
        self.a = "Some Value"

# 将类名赋值给变量
class_A = A

# 使用 class_A 创建类 A 的实例
a = class_A()

在Go语言中是否有类似的机制可以实现这样的操作?我不知道在Go语言的文档中应该查找这样的内容。老实说,通常我不知道编程语言中这些被称为什么。

例如,我想要能够做到以下操作:

// 例如,myType 的类型将是什么?
var myType type = int

我将使用这个机制来接受“类型”参数。例如:

func Infer(value interface{}, type gotype) {...}
英文:

How to have a variable whose type is a variable type?

What do I mean by that? I have a python and java background. In both languages I can do things like assigning a class name to variable.

#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()

Is there such a mechanism in go that allows me to do that? I couldn't know where to look at for such things in their documentation. Honestly, generally I don't know what these are called in programming languages.

For example I would like to be able to do:

// For example, what would be the type of myType?
var myType type = int

I will use this mechanism to take "type" arguments. For example:

func Infer(value interface{}, type gotype) {...}

答案1

得分: 2

在Go语言中,没有直接的机制可以实现你描述的功能。

简短的回答是:没有

长一点的回答是:这听起来像是一个XY问题,所以根据你实际想要实现的目标,可能有其他的方法。请跳到最后看看我是如何解决你具体问题的。

我猜想,如果你想要将一个数据类型存储在一个变量中,可能是因为你要将其他变量的类型与之进行比较,或者你想要创建一个未知类型的变量。在Go语言中,这两种情况都是可以实现的。

在第一种情况下,你可以使用类型切换(type switch):

switch someVar.(type) {
    case string:
        // 做一些字符串操作
    case int:
        // 做一些整数操作
}

从语义上来说,这看起来很像是将someVar的类型赋值给隐式的切换比较变量,然后与各种类型进行比较。

在我提到的第二种情况下,如果你想要创建一个未知类型的变量,可以使用反射(reflection)来实现:

type := reflect.TypeOf(someVar)
newVar := reflect.New(type).Interface()

这样,newVar就是一个包装了与someVar相同类型的变量的interface{}类型。

根据你具体的例子:

我将使用这个机制来接收“类型”参数。例如:

func Infer(value interface{}, type gotype) {...}

不,无法实现这个功能。实际上,这与Go语言的变量支持关系较小,更多地与Go语言是编译型语言有关。

变量完全是运行时的概念。函数签名(就像Go语言中的其他类型一样)在编译时就已经确定了。因此,运行时变量无法影响编译阶段。这在任何编译型语言中都是如此,而不是Go语言的特殊特性(或缺乏特性)。

英文:

> Is there such a mechanism in go that allows me to do that?

The short answer is: No.

The long answer is: This sounds like an XY Problem, so depending on what you're actually trying to accomplish, there may be a way. Jump to the end to see where I address your specific use-case.

I suspect that if you want to store a data type in a variable, it's because you either want to compare some other variable's type against it, or perhaps you want to create a variable of this otherwise unknown type. These two scenarios can be accomplished in Go.

In the first case, just use a type switch:

switch someVar.(type) {
    case string:
        // Do stringy things
    case int:
        // Do inty things
}

Semantically, this looks a lot like assigning the type of someVar to the implicit switch comparison variable, then comparing against various types.

In the second case I mentioned, if you want to create a variable of an unknown type, this can be done in a round-about way using reflection:

type := reflect.TypeOf(someVar)
newVar := reflect.New(type).Interface()

Here newVar is now an interface{} that wraps a variable of the same type as someVar.

In your specific example:

> I will use this mechanism to take "type" arguments. For example:
>
> func Infer(value interface{}, type gotype) {...}

No, there's no way to do this. And it actually has much less to do with Go's variable support than it does with the fact that Go is compiled.

Variables are entirely a runtime concept. Function signatures (like all other types in Go) are fixed at compilation time. It's therefore impossible for runtime variables to affect the compilation stage. This is true in any compiled language, not a special feature (or lack thereof) in Go.

答案2

得分: 1

在Go语言中没有这样的机制。

英文:

> Is there such a mechanism in go that allows me to do that?

No there is not.

答案3

得分: 0

使用空接口:

var x, y interface{}
var a uint32
a = 255
x = int8(a)
y = uint8(a)

Playground 示例

英文:

Use the empty interface:

var x, y interface{}
var a uint32
a = 255
x = int8(a)
y = uint8(a)

Playground example

huangapple
  • 本文由 发表于 2017年7月25日 14:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/45295185.html
匿名

发表评论

匿名网友

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

确定