Skip to content

Instantly share code, notes, and snippets.

@scbizu
Created August 1, 2016 16:42
Show Gist options
  • Save scbizu/d6fab3a7a1840809ce183352f8cd4267 to your computer and use it in GitHub Desktop.
Save scbizu/d6fab3a7a1840809ce183352f8cd4267 to your computer and use it in GitHub Desktop.
struct to json
package main
import (
"encoding/json"
"fmt"
)
type JsonFor struct {
Foo string `json:"foo"`
Foo_s string `json:"foo_s,string"`
Foo_t string `json:"foo_t,omitempty"`
}
func main() {
var foo2 JsonFor
foo := &JsonFor{"a", "b", ""}
test, err := json.Marshal(foo)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(test))
err = json.Unmarshal(test, &foo2)
if err != nil {
fmt.Println(err)
}
fmt.Println(foo2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment