←back to thread

Go subtleties

(harrisoncramer.me)
234 points darccio | 1 comments | | HN request time: 0.228s | source
Show context
username223 ◴[] No.45667467[source]
Go has certainly come a long ways from its initial mission to be a simple language for Rob Pike's simple coworkers.

    type User struct {
        Name     string `json:"name"`
        Password string `json:"-"`
        Email    string `json:"email"`
    }
So you can specify how to serialize a struct in json using raw string literals containing arbitrary metadata. And json:"X" means to serialize it to X, except the special value "-" means "omit this one," except "-," means that its name is "-". Got it.
replies(3): >>45667501 #>>45667570 #>>45672640 #
Cthulhu_ ◴[] No.45667501[source]
I never liked the concept of struct tags, it's a kind of stringly typed programming where the meaning of X or - depends entirely on what the json package says it means.

An alternative is to introduce something like annotations, but I'm sure there will be resistance as it makes the language lean closer to e.g. Java.

But my take on that is that if you want stricter typing like that, you should actually go to Java or C# or whatever.

replies(2): >>45667661 #>>45667828 #
1. bpicolo ◴[] No.45667661[source]
Java resisted first party support of annotations. It was a very controversial addition in the early 2000s

Support for the types of metaprogramming/metadata that annotations are used for is a useful attribute of languages in general