A Go struct tag attaches metadata to a field declaration. Encoders can use that metadata to control how the field appears in serialized data. section 3.1.5
Listing 3.4 Formatting data as JSON
package main
import (
"fmt"
"encoding/json"
)
type Animal struct { #1
Name string `json:"animal_name"` #2
ScientificName string `json:"scientific_name"` #2
Weight float32 `json:"animal_average_weight"` #2
}
func main() {
a := Animal{
Name: "cat",
ScientificName: "Felis catus",
Weight: 10.5,
} #3
output, err := json.Marshal(a)
if err != nil {
panic("couldn't encode json")
}
fmt.Println(string(output)) #4
}
The Animal fields use json tags to choose JSON property names; encoding/json marshals the struct using those names. The fields must be exported for the encoder to access them. section 3.1.5

























