Go语言 之结构体比较与赋值

package main

import (
    "fmt"
)

type Student struct {
    id   int
    name string
}

func main() {
    //比较
    s1 := Student{1, "yy"}
    s2 := Student{2, "yang"}
    s3 := Student{1, "yy"}
    fmt.Println(s1 == s2) //false
    fmt.Println(s1 == s3) //true
    //赋值
    var s4 Student
    s4 = s1
    fmt.Println(s4) //{1 yy}
}

两个结构体可以使用 == 或 != 运算符进行比较,但不支持 > 或 <。

同类型的两个结构体变量可以相互赋值。
参考: https://www.cnblogs.com/yang-2018/p/11118730.html

posted @ 2020-11-20 16:28  Terry-  阅读(299)  评论(0)    收藏  举报