Home > OS >  C# equivalent of @ for variables in Go?
C# equivalent of @ for variables in Go?

Time:12-23

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?

CodePudding user response:

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.

  • Related