Skip to content

Instantly share code, notes, and snippets.

@nikzayn
Created May 25, 2023 08:07
Show Gist options
  • Save nikzayn/ac14b4f8ba4cfe9f4b870e8586a0657d to your computer and use it in GitHub Desktop.
Save nikzayn/ac14b4f8ba4cfe9f4b870e8586a0657d to your computer and use it in GitHub Desktop.
Example of using omitempty tag wihout using pointers in structs
package main
import (
"encoding/json"
"fmt"
)
type BMI struct {
Height int
Weight int
}
type Person struct {
Name string
Age int `json:",omitempty"`
BodyMassIndex BMI `json:",omitempty"`
}
func main() {
p := Person{
Name: "Nikhil",
}
b, _ := json.Marshal(p)
fmt.Println(string(b))
}
//Ouput
- {"Name":"Nikhil","BodyMassIndex":{"Height":0,"Weight":0}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment