Testing Map Equivalence in Go
In table-driven tests, the challenge of testing map equality arises. Manually checking lengths and key-value pairs becomes tedious, especially when repeated for different map types.
Idiomatic Solution
The idiomatic solution utilizes the Go standard library's reflect package. The reflect.DeepEqual function takes in two interface{} arguments and checks for equality by:
Example
import "reflect" func TestMapEquality(t *testing.T) { m1 := map[string]int{"foo": 1, "bar": 2} m2 := map[string]int{"foo": 1, "bar": 2} eq := reflect.DeepEqual(m1, m2) if !eq { t.Errorf("Maps not equal: %v", m1, m2) } }
Additional Notes
The above is the detailed content of How to Test Map Equivalence in Go?. For more information, please follow other related articles on the PHP Chinese website!