Created
May 25, 2023 08:07
-
-
Save nikzayn/ac14b4f8ba4cfe9f4b870e8586a0657d to your computer and use it in GitHub Desktop.
Example of using omitempty tag wihout using pointers in structs
This file contains hidden or 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
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