-
-
Save alextanhongpin/f5ec87c3074f2efbd28180b10d92e9fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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