golang单元测试类示例代码:reflect.DeepEqual比较两个map的值是否相同

细节说明

  • 命名时一定要有test字眼,例如dong_test.go 只有这种命名规范测试通过:xxx_test.go
    在这里插入图片描述
  • 包名可以是其他,不一定要main

测试结果

在这里插入图片描述

代码

package aaa

import (
	"reflect"
	"testing"
)

func TestSlice(t *testing.T) {
    
    
	m1 := map[string]int{
    
    "id": 1, "pid": 0}
	m2 := map[string]int{
    
    "pid": 0, "id": 1}
	//t.Log(m1 == m2)//invalid operation: m1 == m2 (map can only be compared to nil)

	//map变量只能和空(nil)比较
	t.Log(m1 == nil) //false
	t.Log(m2 != nil) //true

	t.Log(reflect.DeepEqual(m1, m2)) //true
}

猜你喜欢

转载自blog.csdn.net/a772304419/article/details/125170610