鉴于这两个测试用例:
func TestEqualWhat(t *testing.T) { testMarshalUnmarshal(t,map[string]interface{}{"a":"b"}) testMarshalUnmarshal(t,map[string]interface{}{"a":5}) }
testMarshalUnmarshal帮助器只是对json进行编组并返回:
func testMarshalUnmarshal(t *testing.T,in map[string]interface{}) { //marshal the object to a string jsb,err := json.Marshal(in); if err != nil { log.Printf("Unable to marshal msg") t.FailNow() } //unmarshal to a map res := make(map[string]interface{}) if err := json.Unmarshal(jsb,&res); err != nil { t.FailNow() } if !reflect.DeepEqual(in,res) { log.Printf("\nExpected %#v\nbut got %#v",in,res) t.FailNow() } }
为什么第一个测试用例通过而第二个测试用例失败?测试的输出是这样的:
Expected map[string]interface {}{"a":5} but got map[string]interface {}{"a":5} --- FAIL: TestEqualWhat (0.00 seconds)
Here is similar code on the go playground所以你可以轻松搞定它.
原文地址:https://www.jb51.cc/go/186868.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。