C# equivalent of @ for variables in Go?

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

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.

huangapple
  • 本文由 发表于 2021年12月22日 18:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70447373.html
匿名

发表评论

匿名网友

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

确定