英文:
C# equivalent of @ for variables in Go?
问题
在Go语言中,当我们想要使用与关键字相同的变量名时,我们可以在变量名前加上_
下划线。
type_ := "Hello, world"
这样就可以避免与关键字冲突。
英文:
In C# when we want to have a variable with the same name as a keyword, we can prefix the var with @.
var @type = "Hello, world";
Is there anything similar in Go?
答案1
得分: 3
在Golang中有类似的东西吗?
没有。你不能重新声明关键字。type
是一个关键字。
尽管有一些预声明的标识符,它们不是关键字,你可以在较小的作用域中隐藏它们。(顺便说一下,可以 != 应该)
var b bool = true
func main() {
bool := "shadowed bool ident of type string"
fmt.Println(bool)
}
我最常见到的变量命名为"type"的模式是使用typ
代替。
请注意,导出的标识符,例如Type
,是有效的。
英文:
> Is there anything similar in Golang?
No. You can't redeclare keywords. type
is a keyword.
Though some identifiers are predeclared, they are not keywords, and you can shadow them in a lesser scope. (by the way, can != should)
var b bool = true
func main() {
bool := "shadowed bool ident of type string"
fmt.Println(bool)
}
The pattern I see most commonly for variables named "type" is to use typ
instead.
Note that exported identifiers, e.g. Type
, are valid.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论