How to test the equivalence of maps in Golang?

前端 未结 8 1527
情歌与酒
情歌与酒 2021-01-30 00:55

I have a table-driven test case like this one:

func CountWords(s string) map[string]int

func TestCountWords(t *testing.T) {
  var tests = []struct {
    input s         


        
8条回答
  •  太阳男子
    2021-01-30 01:03

    Disclaimer: Unrelated to map[string]int but related to testing the equivalence of maps in Go, which is the title of the question

    If you have a map of a pointer type (like map[*string]int), then you do not want to use reflect.DeepEqual because it will return false.

    Finally, if the key is a type that contains an unexported pointer, like time.Time, then reflect.DeepEqual on such a map can also return false.

提交回复
热议问题