Created
December 4, 2015 22:41
-
-
Save liggitt/51924670d6b78cd5ee72 to your computer and use it in GitHub Desktop.
omitempty behavior with zeros, nulls, and numbers
This file contains 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" | |
) | |
func main() { | |
zero := 0 | |
one := 0 | |
empty, _ := json.Marshal(A{}) | |
fmt.Println(string(empty)) | |
zeros, _ := json.Marshal(A{Int: zero, IntPointer: &zero, IntOmit: zero, IntPointerOmit: &zero}) | |
fmt.Println(string(zeros)) | |
ones, _ := json.Marshal(A{Int: one, IntPointer: &one, IntOmit: one, IntPointerOmit: &one}) | |
fmt.Println(string(ones)) | |
} | |
type A struct { | |
Int int `json:"int"` | |
IntPointer *int `json:"intPointer"` | |
IntOmit int `json:"intOmit,omitempty"` | |
IntPointerOmit *int `json:"intPointerOmit,omitempty"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://play.golang.org/p/_QO8RlCVof