Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# equivalent of @ for variables in Go?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading