Thank Dave and Jan. I understood completely. I'd consider another solution to manage that next.
Thanks again. Harry. 2016年6月21日火曜日 14時58分39秒 UTC+9 Dave Cheney: > > To serialise the keys and values of a map and code would have to iterate > over it > > for k,v := range m { > // serialise k and v > } > > But map iteration does not have a guaranteed ordering. This is why the > result is not stable. > > On Tuesday, 21 June 2016 15:44:05 UTC+10, Harry wrote: >> >> Hello guys, >> >> I'm trying to use encoding/gob package to know how it works. >> And I wrote below codes. >> >> >> >> import "encoding/gob" >> >> >> func ToGOB64(data interface{}) (string, error) { >> b := bytes.Buffer{} >> >> e := gob.NewEncoder(&b) >> err := e.Encode(data) >> if err != nil { >> fmt.Println(`failed gob Encode`, err) >> return "", err >> } >> return base64.StdEncoding.EncodeToString(b.Bytes()), nil >> >> } >> >> >> >> func FromGOB64(str string, tData interface{}) error { >> >> by, err := base64.StdEncoding.DecodeString(str) >> >> if err != nil { >> return err >> >> } >> b := bytes.Buffer{} >> b.Write(by) >> d := gob.NewDecoder(&b) >> err = d.Decode(tData) >> if err != nil { >> return err >> >> } >> return nil >> } >> >> >> And then, I prepared for test targeted type are struct, map, interface{}. >> >> func TestSerializeStruct(t *testing.T) { >> u := User{Id: 10, Name: "harry dayo"} >> >> result, err := ToGOB64(u) >> if err != nil { >> t.Errorf("TestSerializeStruct error: %s", err) >> } >> if result != >> "Iv+BAwEBBFVzZXIB/4IAAQIBAklkAQQAAQROYW1lAQwAAAAR/4IBFAEKaGFycnkgZGF5bwA=" >> { >> t.Errorf("TestSerializeStruct result: %+v", result) >> } >> } >> >> >> >> func TestDeSerializeStruct(t *testing.T) { >> >> >> u := User{} >> err := FromGOB64( >> "Iv+BAwEBBFVzZXIB/4IAAQIBAklkAQQAAQROYW1lAQwAAAAR/4IBFAEKaGFycnkgZGF5bwA=" >> , &u) >> if err != nil { >> t.Errorf("TestDeSerializeStruct error: %s", err) >> } >> if u.Id != 10 { >> t.Errorf("TestDeSerializeStruct result: %+v", u) >> } >> } >> >> >> //2.map (map is not stable.) >> func TestSerializeMap(t *testing.T) { >> >> >> m := map[string]int{"apple": 150, "banana": 300, "lemon": 300} >> //when using map, result is not stable. >> result, err := ToGOB64(m) >> if err != nil { >> t.Errorf("TestSerializeMap error: %s", err) >> } >> if result != >> "Dv+DBAEC/4QAAQwBBAAAIP+EAAMFYXBwbGX+ASwGYmFuYW5h/gJYBWxlbW9u/gJY" { >> //if result != >> "Dv+DBAEC/4QAAQwBBAAAIP+EAAMGYmFuYW5h/gJYBWxlbW9u/gJYBWFwcGxl/gEs" { >> t.Errorf("TestSerializeMap result: %#v", result) >> } >> } >> >> >> // >> func TestDeSerializeMap(t *testing.T) { >> >> >> u := map[string]int{} >> >> //FromGOB64("Dv+DBAEC/4QAAQwBBAAAIP+EAAMFYXBwbGX+ASwGYmFuYW5h/gJYBWxlbW9u/gJY", >> >> &u) >> err := FromGOB64( >> "Dv+DBAEC/4QAAQwBBAAAIP+EAAMGYmFuYW5h/gJYBWxlbW9u/gJYBWFwcGxl/gEs", &u) >> if err != nil { >> t.Errorf("TestDeSerializeMap error: %s", err) >> } >> if u["apple"] != 150 { >> t.Errorf("TestDeSerializeMap result: %#v", u) >> } >> } >> >> >> >> //interface x slice >> func TestSerializeInterfaces(t *testing.T) { >> >> >> args := []interface{}{1, "abcde", true} >> result, err := ToGOB64(args) >> if err != nil { >> t.Errorf("TestSerializeInterfaces error: %s", err) >> } >> if result != >> "DP+BAgEC/4IAARAAACX/ggADA2ludAQCAAIGc3RyaW5nDAcABWFiY2RlBGJvb2wCAgAB" { >> t.Errorf("TestSerializeInterfaces result: %+v", result) >> } >> } >> >> >> >> func TestDeSerializeInterfaces(t *testing.T) { >> >> >> args := []interface{}{} >> err := FromGOB64( >> "DP+BAgEC/4IAARAAACX/ggADA2ludAQCAAIGc3RyaW5nDAcABWFiY2RlBGJvb2wCAgAB", & >> args) >> if err != nil { >> t.Errorf("TestDeSerializeInterfaces error: %s", err) >> } >> if args[0] != 1 || args[1] != "abcde" || args[2] != true { >> t.Errorf("TestDeSerializeInterfaces result: %+v", args) >> } >> } >> >> >> >> As a result, serialized map value often change though deserialized map is >> no problem. >> Why serialized value is not invaliable when using map value. >> >> I'd like to know that reason sincerely. >> >> Thanks in advance. >> >> >> >> >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.