在Go语言的类型切换(Type Switch)中,变量”t”的实际类型是什么?

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

What is the actual type of the variable "t" in a GoLang Type Switch?

问题

给定代码中,变量t在声明时的实际类型是什么?为了说明这个问题:如果我想写

var t //我应该使用什么类型?!

我应该如何声明t

在Go语言中,我相信我正在寻找类似于TTypeKind的东西,它具有类似于tkIntegertkString等的潜在值。

在C++中,我们可以找到type_info类类型,它也提供了一些这样的功能。

var t...声明中,我应该使用什么类型?这种情况是否可能?

英文:

Given:

type MyInterface interface{

    MyMethod() int

}

var im MyInterface
...

If I call:

switch t := im.(type) {

   case:....

   default:...

}

What is the actual type of the variable t when it is declared? To illustrate the question: If I wanted to write

var t //What type should I use?! 

How would I declare t ?

For example In Delphi we have this structure:

TTypeInfo = record
  Kind: TTypeKind;
  Name: string;
  {TypeData: TTypeData}
end;

I believe I am looking for something in GoLang that is akin to TTypeKind, which has potential values such as tkInteger, tkString etc.

In C++, we find the type_info class type, which also provides some of this functionality.

What type would I use for t in the var t... declaration? Is this even possible?

答案1

得分: 4

在Go语言中,类型切换是一种奇怪而特殊的机制。它不是应用于类型的另一种形式的泛化。

据我所知,你不能使用var来编写它(我知道你只是将var形式作为示例)。

switchcase子句中,t的类型是在该case中指定的(参见@Mue的答案)。在default子句中,它具有im的原始类型。

回答你的问题,这种情况下的t符号是奇怪而特殊的,只有在switch的上下文中才有意义。它的类型取决于上下文。我甚至不敢称其为变量。

编辑,推测:

我没有查看编译器,但我怀疑它将每个case解构为类型断言(类型转换),其结果是case子句范围内的局部变量。作为一种语法上的便利,你可以使用t符号引用每个变量。

英文:

The type switch in Go is weird and special business. It’s not a generalization of another form that happens to be applied to types.

To my knowledge, you can’t write it using var (and I do recognize that you only mention the var form as illustration).

Within a case clause of the switch, t has the type specified in that case (see @Mue’s answer). Inside the default clause, it has im’s original type.

To answer your question, the t symbol in this case is weird and special, and only meaningful in the context of the switch. Its type is context-dependent. I hesitate to even call it a variable.

Edit, speculation:

I haven’t looked at the compiler but I suspect it deconstructs each case into a type assertion (cast), the result of which is a local variable within the scope of the case clause. As a syntactic convenience, you can refer to each using the t symbol.

答案2

得分: 3

在下面的代码行中:

switch t := im.(type) {

t已经被声明和初始化为类型的值,因此你不需要再使用var t重新声明它。

英文:

In the following line:

switch t := im.(type) {

t is already declared & initialized with the value of the type, therefore you don't have to declare it again using var t.

答案3

得分: 2

这是要翻译的内容:

这很简单,不需要声明。

switch t := anyVar.(type) {
case ThisType:
    // t 是 ThisType 类型
case ThatType:
    // t 是 ThatType 类型
default:
    // t 保持原始类型
}

顺便说一下,请不要在接口前加上 I 前缀,这不符合 Go 的风格。通常你可以选择一个与方法的动词匹配的主题。参见

type Writer interface {
    Write([]byte) (int, error)
}
英文:

It is simple and needs no declaring.

switch t := anyVar.(type) {
case ThisType:
    // t is of type ThisType
case ThatType:
    // t is of type ThatType
default:
    // t has its original type.
}

And by the way, please no prefix I for an interface, that's no Go style. Typically you choose a subject matching to the verb(s) of the method(s). See

type Writer interface {
    Write([]byte) (int, error)
}

huangapple
  • 本文由 发表于 2014年2月21日 18:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/21932078.html
匿名

发表评论

匿名网友

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

确定