typora/daliy_note/8月归档/8.23/struct_compare.md
2024-12-12 10:48:55 +08:00

683 B

  • 别名可以直接比较 type MyStudent1 = Student
  • 属性字段一样,可以通过类型转换进行比较
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)))
}