Skip to content

Instantly share code, notes, and snippets.

@zetaab
Created October 9, 2018 10:22
Show Gist options
  • Save zetaab/2a0b7381f75e79c56173c6af56292ca7 to your computer and use it in GitHub Desktop.
Save zetaab/2a0b7381f75e79c56173c6af56292ca7 to your computer and use it in GitHub Desktop.
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 main() {
test := Test{
SlowRampTime: 0,
}
marshalJSON, err := jsonMarshal(test)
if err != nil {
fmt.Errorf(err.Error())
}
fmt.Printf("json: %s", string(marshalJSON))
test2 := Test{
SlowRampTime: 1,
}
marshalJSON2, err := jsonMarshal(test2)
if err != nil {
fmt.Errorf(err.Error())
}
fmt.Printf("json: %s", string(marshalJSON2))
}
% go run main.go
json: {}
json: {"slowRampTime":1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment