Created
October 9, 2018 11:05
-
-
Save zetaab/718acf7506574cf2acedf589d2bcea4e to your computer and use it in GitHub Desktop.
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 ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
) | |
type Test struct { | |
SlowRampTime *int `json:"slowRampTime,omitempty"` | |
} | |
func jsonMarshal(t interface{}) ([]byte, error) { | |
buffer := &bytes.Buffer{} | |
encoder := json.NewEncoder(buffer) | |
encoder.SetEscapeHTML(false) | |
err := encoder.Encode(t) | |
return buffer.Bytes(), err | |
} | |
func intToPointer(integer int) *int { | |
var i int | |
i = integer | |
return &i | |
} | |
func main() { | |
test := Test{ | |
SlowRampTime: intToPointer(0), | |
} | |
marshalJSON, err := jsonMarshal(test) | |
if err != nil { | |
fmt.Errorf(err.Error()) | |
} | |
fmt.Printf("json: %s", string(marshalJSON)) | |
test2 := Test{ | |
SlowRampTime: intToPointer(1), | |
} | |
marshalJSON2, err := jsonMarshal(test2) | |
if err != nil { | |
fmt.Errorf(err.Error()) | |
} | |
fmt.Printf("json: %s", string(marshalJSON2)) | |
test3 := Test{ | |
} | |
marshalJSON3, err := jsonMarshal(test3) | |
if err != nil { | |
fmt.Errorf(err.Error()) | |
} | |
fmt.Printf("json: %s", string(marshalJSON3)) | |
} | |
% go run main.go | |
json: {"slowRampTime":0} | |
json: {"slowRampTime":1} | |
json: {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment