go之Marshal

时间:2025-02-23 07:55:16

        看代码:

package main

import (
    "fmt"
    "encoding/json"
)

func test1(){
    m := map[string]interface{} {"name":"taoge", "age":30, "addr":"China"}
    (m)

    data, _ := (m)
    (string(data))

    m1 := make(map[string]interface{})
    _ = (data, &m1)
    (m1)
}

func test2(){
    type Person struct {
        Name string `json:"name"`
        Age  int    `json:"age"`
        Addr string `json:"addr"`
    }
    
    p1 := Person{
        Name: "taoge",
        Age:  30,
        Addr: "China",  // oh my god, this comma cannot be omitted
    }
    
    (p1)

    data, _ := (p1)
    (string(data))
    
    var p2 Person 
    _ = (data, &p2)
    (p2)
}

func main() {
    test1()
    ("-------------")
    test2()
}

         结果:

map[name:taoge age:30 addr:China]
{"addr":"China","age":30,"name":"taoge"}
map[addr:China age:30 name:taoge]
-------------
{taoge 30 China}
{"name":"taoge","age":30,"addr":"China"}
{taoge 30 China}

 

        简单, 不需要说。