JSON and Data Exchange
Optional Fields
Optional JSON fields can be included only when a value is present.
optional field
An optional JSON field can be present for one value and absent for another while the Go type stays the same.
Optional Fields
optional_fields.go
Replay: real traced execution (multi-file project)
package main
import (
"fmt"
"strings"
)
type Profile struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
}
func main() {
var includeEmail = true
profile := Profile{Name: "Mina"}
if includeEmail {
profile.Email = "mina@example.com"
}
text := `{"name":"` + profile.Name + `"}`
if profile.Email != "" {
text = `{"name":"` + profile.Name + `","email":"` + profile.Email + `"}`
}
hasEmail := strings.Contains(text, "email")
fmt.Println("includeEmail=", includeEmail)
fmt.Println("hasEmail=", hasEmail)
fmt.Println("json=", text)
}
package main
import (
"fmt"
"strings"
)
type Profile struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
}
func main() {
var includeEmail = false
profile := Profile{Name: "Mina"}
if includeEmail {
profile.Email = "mina@example.com"
}
text := `{"name":"` + profile.Name + `"}`
if profile.Email != "" {
text = `{"name":"` + profile.Name + `","email":"` + profile.Email + `"}`
}
hasEmail := strings.Contains(text, "email")
fmt.Println("includeEmail=", includeEmail)
fmt.Println("hasEmail=", hasEmail)
fmt.Println("json=", text)
}
includeEmail ← true, profile ← main.Profile{Name:"Mina", Email:""}
13func main() {14 var includeEmail→ true = true //@includeEmail=false15 profile→ main.Profile{Name:"Mina", Email:""} := Profile{Name: "Mina"}profile.Email ← "mina@example.com"
17if includeEmailtrue {18 profile.Email→ "mina@example.com" = "mina@example.com"19}text ← "{\"name\":\"Mina\"}"
21text→ "{\"name\":\"Mina\"}" := `{"name":"` + profile.Name"Mina" + `"}`22if profile.Email != "" {text ← "{\"name\":\"Mina\",\"email\":\"mina@example.com\"}"
21text := `{"name":"` + profile.Name + `"}`22if profile.Email"mina@example.com" != "" {23 text→ "{\"name\":\"Mina\",\"email\":\"mina@example.com\"}" = `{"name":"` + profile.Name"Mina" + `","email":"` + profile.Email"mina@example.com" + `"}`24}hasEmail ← true
26 hasEmail→ true := strings.Contains(text"{\"name\":\"Mina\",\"email\":\"mina@example.com\"}", "email")27 fmt.Println("includeEmail=", includeEmailtrue)28 fmt.Println("hasEmail=", hasEmailtrue)29 fmt.Println("json=", text"{\"name\":\"Mina\",\"email\":\"mina@example.com\"}")30}outputincludeEmail= true hasEmail= true json= {"name":"Mina","email":"mina@example.com"}
includeEmail ← false, profile ← main.Profile{Name:"Mina", Email:""}
13func main() {14 var includeEmail→ false = false15 profile→ main.Profile{Name:"Mina", Email:""} := Profile{Name: "Mina"}1617 if includeEmail {18 profile.Email = "mina@example.com"19 }2021 text→ "{\"name\":\"Mina\"}" := `{"name":"` + profile.Name"Mina" + `"}`22 if profile.Email != "" {23 text = `{"name":"` + profile.Name + `","email":"` + profile.Email + `"}`24 }2526 hasEmail→ false := strings.Contains(text"{\"name\":\"Mina\"}", "email")27 fmt.Println("includeEmail=", includeEmailfalse)28 fmt.Println("hasEmail=", hasEmailfalse)29 fmt.Println("json=", text"{\"name\":\"Mina\"}")30}outputincludeEmail= false hasEmail= false json= {"name":"Mina"}