英文:
What is the actual type of the variable "t" in a GoLang Type Switch?
问题
给定代码中,变量t
在声明时的实际类型是什么?为了说明这个问题:如果我想写
var t //我应该使用什么类型?!
我应该如何声明t
?
在Go语言中,我相信我正在寻找类似于TTypeKind
的东西,它具有类似于tkInteger
、tkString
等的潜在值。
在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
形式作为示例)。
在switch
的case
子句中,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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论