package main import ( "encoding/json" "fmt" ) func main() { m := make(map[string]interface{}, 4) //因为结构体的键名都是字符串类型,但是值有很多类型,这里就使用interface{}万能类型,容量为4 m["company"] = "zyg" m["subject"] = []string{"go", "python", "java"} m["isok"] = true m["price"] = 5.55 result, err := json.Marshal(m) if err != nil { return } fmt.Println("result = ", string(result)) }
执行结果
result = {"company":"zyg","isok":true,"price":5.55,"subject":["go","python","java"]}