golang 结构体比较

时间:2024-04-08 19:19:23

1.如果结构体成员包含 map, slice, 不可比较
2.int string 普通类型,如果变量顺序不一样,则不可比较

package main

import "fmt"

func main() {
	s1 := struct {
		//a map[int]int
		// a []int
		a string
		b int
	}{}
	s2 := struct {
		//a map[int]int
		// a []int
		b int
		a string
	}{}

	fmt.Println(s1 == s2)
}



invalid operation: cannot compare s1 == s2 (struct containing map[int]int cannot be compared
invalid operation: cannot compare s1 == s2 (struct containing []int cannot be compared)