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