Skip to content

Instantly share code, notes, and snippets.

@laidback
Created April 28, 2018 21:30
Show Gist options
  • Select an option

  • Save laidback/4394b2ac753957e81a44460542e3046f to your computer and use it in GitHub Desktop.

Select an option

Save laidback/4394b2ac753957e81a44460542e3046f to your computer and use it in GitHub Desktop.
go-json created by laidback - https://repl.it/@laidback/go-json
package main
import (
"encoding/json"
"fmt"
)
type resp struct {
Fname string `json:"fname, string"`
}
func main() {
// go struct
r := &resp{Fname: "lukas"}
fmt.Printf("%T, %s\n", r, r)
// marshalling to []byte
m, _ := json.Marshal(r)
fmt.Printf("%T, %s, %x\n", m, m, m)
// json string
str := `{"fname": "lukas"}`
pers := &resp{}
// unmarshal json string
if err := json.Unmarshal([]byte(str), &pers); err != nil {
fmt.Println(err)
}
fmt.Printf("%T, %s\n", pers, pers)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment