lengyijun
7/17/2018 - 8:45 AM

a problem in marshal and unmarshal in Golang json processing.md

package main

import (
    "encoding/json"
    "fmt"

    "github.com/davecgh/go-spew/spew"
)

type gene struct {
    data  string
    Index int
}

func main() {
    a := &gene{
        data:  "fdasfdsaf",
        Index: 1,
    }
    a_byte, err := json.Marshal(a)
    if err != nil {
        fmt.Println("error")
    }
    spew.Dump(a_byte)

    c := gene{}
    err = json.Unmarshal(a_byte, &c)
    if err != nil {
        fmt.Println("error")
    }
    spew.Dump(c)

}

the output

([]uint8) (len=11 cap=64) {
 00000000  7b 22 49 6e 64 65 78 22  3a 31 7d                 |{"Index":1}|
}
(main.gene) {
 data: (string) "",
 Index: (int) 1
}