typora/daliy_note/8月归档/8.23/struct_compare.md

35 lines
683 B
Markdown
Raw Permalink Normal View History

2024-12-11 21:48:55 -05:00
- 别名可以直接比较 type MyStudent1 = Student
- 属性字段一样,可以通过类型转换进行比较
```Go
package main
import "fmt"
type Student struct {
Name string
}
type StudentAnother struct {
Name string
}
type MyStudent Student
type MyStudent1 = Student
func main() {
s1 := Student{Name: "1"}
s2 := MyStudent{Name: "1"}
s3 := MyStudent1{Name: "1"}
s4 := StudentAnother{Name: "1"}
fmt.Printf("s1: %T\n", s1)
fmt.Printf("s2: %T\n", s2)
fmt.Printf("s3: %T\n", s3)
fmt.Printf("s4: %T\n", s4)
// fmt.Printf("(s1 == s2): %v\n", (s1 == s2))
fmt.Printf("(s1 == s3): %v\n", (s1 == s3))
fmt.Printf("(s1 == Student(s4)): %v\n", (s1 == Student(s4)))
}
```