DEV Community

loading...

Discussion on: Language Features: Best and Worst

Collapse
pretzelhands profile image
pretzelhands

The property tags in Go:

type Invitation struct {
    InvitationToken string `json:"invitationToken" db:"invitation_token"`
}

The fact that I can have a variable called InvitationToken in Go, that is called invitationToken in JSON and invitation_token in my database is just absolutely amazing. It resolves one of my biggest pet peeves in programming, which is mixing naming cases.

Collapse
awwsmm profile image
Andrew (he/him) Author

This is basically just metadata that you can attach to a variable, right?

related: stackoverflow.com/questions/108587...

Collapse
pretzelhands profile image
pretzelhands

That's the gist of it yes. I think you could implement the same thing with tagged template literals in JS, but Go just offers it natively. 🤓

Thread Thread
vberlier profile image
Valentin Berlier

In js you would use decorators, which are native langage features:

class Invitation {
  @Serialize({ json: 'invitationToken', db: 'invitation_token' })
  token
}
Forem Open with the Forem app