Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Last active September 9, 2017 01:22
Show Gist options
  • Save alextanhongpin/f5ec87c3074f2efbd28180b10d92e9fc to your computer and use it in GitHub Desktop.
Save alextanhongpin/f5ec87c3074f2efbd28180b10d92e9fc to your computer and use it in GitHub Desktop.
// This program demonstrates how to overwrite the json tag in the struct
package main
import (
"encoding/json"
"log"
)
type BadField struct {
Name string `json:"NameString"`
}
type GoodField struct {
*BadField
BadName string `json:"NameString,omitempty"`
Name string `json:"name"`
}
func main() {
b := BadField{"john.doe"}
g := GoodField{
BadField: &b,
Name: b.Name,
}
out, err := json.Marshal(g)
if err != nil {
log.Printf("error unmarshalling: %v\n", err)
}
log.Println(string(out))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment