Golang中的struct能不能比较

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang中的struct能不能比较相关的知识,希望对你有一定的参考价值。

参考技术A 可以能,也可以不能

可以能,也可以不能

T2和T3是不同的结构体,但可以强制转换,所以强转之后可以比较

如果成员变量含有不可比较成员变量,即使可以强制转换,也不可以比较

可以,也不可以

struct必须是可比较的,才能作为key,否则编译时报错

golang 将json映射到GO中的struct,用于直接编组,unmarshal

package main

 
import "encoding/json"
import "fmt"

 
type Person struct {
    Name string   `json:"name"`

    Age  int      `json:"age,omitempty"`
    Addr *Address `json:"address,omitempty"`

    Ph   []string `json:"phone,omitempty"`

}

//Astea-s faine aici ca imi definesc corespondenta, si cand fac unmarshall, 
//merge blana, plus ca am si validarile alea, p-asta da-l deoparte daca e empty, 
//ca nu ma intereseaza. 

type Address struct {
    Street string `json:"street"`

    City   string `json:"city"`
    State  string `json:"state"`

    Zip    string `json:"zip"`
}

 
func main() {
    // compare with output, note apt field ignored, missing fields

    // have zero values.
    jData := []byte(`{

        "name": "Smith",
        "address": {
            "street": "21 2nd Street",
            "apt": "507",
            "city": "New York",

            "state": "NY",
            "zip": "10021"
        }
    }`)
    var p Person

    err := json.Unmarshal(jData, &p)
    if err != nil {

        fmt.Println(err)
    } else {
        fmt.Printf("%+v\n  %+v\n\n", p, p.Addr)

    }
 
    // compare with output, note empty fields omitted.
    pList := []Person{

        {
            Name: "Jones",
            Age:  21,

        },
        {
            Name: "Smith",
            Addr: &Address{"21 2nd Street", "New York", "NY", "10021"},

            Ph:   []string{"212 555-1234", "646 555-4567"},

        },
    }
    jData, err = json.MarshalIndent(pList, "", "    ")

    if err != nil {
        fmt.Println(err)

    } else {
        fmt.Println(string(jData))

    }
}
/*
Output:
{Name:Smith Age:0 Addr:0xf840026080 Ph:[]}
  &{Street:21 2nd Street City:New York State:NY Zip:10021}

[
    {
        "name": "Jones",
        "age": 21
    },
    {
        "name": "Smith",
        "address": {
            "street": "21 2nd Street",
            "city": "New York",
            "state": "NY",
            "zip": "10021"
        },
        "phone": [
            "212 555-1234",
            "646 555-4567"
        ]
    }
]
*/

//Noi avem impachetarea json-ului facuta at labam, in multe locuri, si ar merge pe varianta asta, 
//sa imi definesc corespondenta. Imi fac o structura mare, care este comuna tuturor response-urilor, 
//si detaliile le lipesc de mana. Nu peste tot, ca de multe ori sunt proxy, si dau exact cum primesc 
//dinspre client spre couch si invers, dar unde fac procesare, cred ca ar fi mai eleganta varianta asta, 
//decat ce facem noi cu map-uri si impachetare json de mana. Ce ziceti?

以上是关于Golang中的struct能不能比较的主要内容,如果未能解决你的问题,请参考以下文章

Golang基础_07-结构struct

Golang关于Go中的struct{}

GoLang struct的tag

golang中的struct标签tag

golang go中的样本整数struct容器

Golang中的构造函数