What are struct tags in Go for and how to use them?
Hello everyone! I recently started learning Go and came across something called struct tags. I see that in code examples they often write something like:
```go
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
Or also:
```go
type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
```
I understand that these are some kind of metadata in backticks, but what exactly are they for? The code works without them too. I tried removing the tags — and the program compiled, but when working with JSON, my fields started being named with a capital letter, not the way I wanted.
Please explain for beginners: what are struct tags, why were they invented at all, and in what real situations are they used? I'm especially interested in how to
No answers yet.
Log in to leave an answer
Blogs