Struct Tags for Encoding/Decoding Data
·
190 words
·
1 minute read
In Go, we use structs to define and group data, as we would use classes in other OOP languages. However, in Go the naming of attributes within the struct is important because if it starts with a lower-case it’s seen as private and with an upper-case first letter it’s seen as public.
We can encode these structs into data formats like json but we might want to rename the fields, struct tags allow us to do this. They are defined using back ticks after the type declaration.
|
|
Multiple Tags: For when you need to use multiple tags they can be separated by spaces.
|
|
Ignore Empty:
When encoding to JSON, we might not want to print out data with hasn’t been defined. To do this we use the omitempty
tag within the name of the item.
|
|