go判断两个字典是否相等

基于go的map是使用HashMap,所以是无序的,如果直接使用==来判断是否相等,则会得到报错:Invalid operation: mp1==mp2 (the operator == is not defined on map[int]int)
需要使用reflect.DeepEqual:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	mp1 := map[int]int{
		12: 23,
		13: 24,
		11: 25,
	}
	mp2 := map[int]int{
		11: 25,
		12: 23,
		13: 24,
	}
	if reflect.DeepEqual(mp1, mp2) {
		fmt.Println("yes")
	} else {
		fmt.Println("no")
	}

}

你可能感兴趣的:(go,golang,c++,算法)