Golang 的 map,slic 转 json 注意 chan

// You can edit this code!
// Click here and start typing.
package main


import "fmt"
import "encoding/json"


type ChannelOperator struct {
Oid      string
Aid      string
Name     string
Isonline bool
Msgchan  chan string  `json:"-"`
}


type ChannelOperators struct {
Op []ChannelOperator
}


func main() {
fmt.Println("Hello, 世界")
mm := make(map[string]*ChannelOperator)
mm["123"] = &ChannelOperator{Oid: "12312"}
mm["5678"] = &ChannelOperator{Oid: "12312"}


b, _ := json.Marshal(mm)
fmt.Println(string(b))


c := ChannelOperators{}
c.Op = append(c.Op, ChannelOperator{Oid: "12312"})


x, err := json.Marshal(c)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(x))

}


Hello, 世界
{"123":{"Oid":"12312","Aid":"","Name":"","Isonline":false},"5678":{"Oid":"12312","Aid":"","Name":"","Isonline":false}}
{"Op":[{"Oid":"12312","Aid":"","Name":"","Isonline":false}]}

你可能感兴趣的:(golang)