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}
}

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

同类型的两个结构体变量可以相互赋值。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM