Skip to content

Instantly share code, notes, and snippets.

@nikzayn
Created May 25, 2023 08:09
Show Gist options
  • Save nikzayn/4edd4614fb45ea222aaa330da5cc0c07 to your computer and use it in GitHub Desktop.
Save nikzayn/4edd4614fb45ea222aaa330da5cc0c07 to your computer and use it in GitHub Desktop.
Example of using omitempty tag 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"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment