From 241666bc1176844a961438ea067db61cd25aae4a Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 9 Feb 2019 07:43:37 -0800 Subject: [PATCH] Example of struct encoding with an ommitted field --- 2019/gojsoncookbook/struct-omit-field.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2019/gojsoncookbook/struct-omit-field.go diff --git a/2019/gojsoncookbook/struct-omit-field.go b/2019/gojsoncookbook/struct-omit-field.go new file mode 100644 index 00000000..eb61ff18 --- /dev/null +++ b/2019/gojsoncookbook/struct-omit-field.go @@ -0,0 +1,22 @@ +// Ommitting a struct field when encoding it. +// +// Eli Bendersky [https://eli.thegreenplace.net] +// This code is in the public domain. +package main + +import ( + "encoding/json" + "fmt" +) + +type Account struct { + Name string + Password string `json:"-"` + Balance float64 +} + +func main() { + joe := Account{Name: "Joe", Password: "123456", Balance: 102.4} + s, _ := json.Marshal(joe) + fmt.Println(string(s)) +}