How to define multiple tags for struct fields in Go?
Hello everyone! I just started learning Go and encountered a problem when working with structs and JSON. I have a struct that describes a user. I want the `Name` field to be serialized as `"full_name"` in JSON, and when deserializing from JSON (e.g., from an external API), it should also accept the `"username"` field. But I can't figure out how to set multiple tags for a single field.
Here's an example of my code:
```go
type User struct {
Name string `json:"full_name"`
Age int `json:"age"`
}
```
I tried writing something like `Name string json:"full_name" json:"username"`, but the compiler complains about duplicate tags. Then I read that you can use backticks and separate tags with spaces: `Name string json:"full_name" xml:"name"`, but these are different formats, and I
No answers yet.
Log in to leave an answer
Blogs