标签:[] ret roc int images proc ofo image process
Json数据协议package main
import (
"encoding/json"
"fmt"
)
type User struct {
UserName string `json:"user_name"`
NickName string `json:"nick_name"`
Age int
Birthday string
Sex string
Email string
Phone string
}
func testStruct() {
user1 := User{
UserName:"user1",
NickName: "阿三",
Age: 18,
Birthday: "1999-9-9",
Sex: "男",
Email: "1234567890@qq.com",
Phone: "666666",
}
data, err := json.Marshal(user1)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
func testMap() {
var mp map[string]interface{}
mp = make(map[string]interface{})
//因为map的值是空接口,所以值可以是任何类型
mp["username"] = "user1"
mp["age"] = 16
mp["sex"] = "女"
mp["score"] = 99.5
data, err := json.Marshal(mp)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
func testSlice() {
var s []map[string]interface{}
var mp map[string]interface{}
mp = make(map[string]interface{})
//因为map的值是空接口,所以值可以是任何类型
mp["username"] = "user66"
mp["age"] = 17
mp["sex"] = "女"
mp["score"] = 100
s = append(s, mp)
//因为map的值是空接口,所以值可以是任何类型
mp["username"] = "user88"
mp["age"] = 18
mp["sex"] = "男"
mp["score"] = 10
s = append(s, mp)
data, err := json.Marshal(s)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
func main() {
testStruct()
testMap()
testSlice()
}
Process finished with exit code 0
标签:[] ret roc int images proc ofo image process
原文地址:http://blog.51cto.com/5660061/2347953