Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这些看似相同的Golang结构不相等?_Go_Testing - Fatal编程技术网

为什么这些看似相同的Golang结构不相等?

为什么这些看似相同的Golang结构不相等?,go,testing,Go,Testing,我正在努力学习Golang测试 我在比较两个看似相同的结构,但它们并不相等。出什么事了 2020/01/22 17:10:10 ****2 cities[0] is type *main.City has &main.City{Name:"Boston", State:"", Country:"USA", Capital:true, Population:685000} 2020/01/22 17:10:10 ****2 expected is type *main.City has

我正在努力学习Golang测试

我在比较两个看似相同的结构,但它们并不相等。出什么事了

2020/01/22 17:10:10 ****2 cities[0] is type *main.City has &main.City{Name:"Boston", State:"", Country:"USA", Capital:true, Population:685000} 
2020/01/22 17:10:10 ****2 expected  is type *main.City has &main.City{Name:"Boston", State:"", Country:"USA", Capital:true, Population:685000} 
2020/01/22 17:10:11 Preparing to DELETE 5 city docs
--- FAIL: TestCities (3.08s)
    --- FAIL: TestCities/Test_POST_should_add_New_City (0.62s)
        city_handlers_test.go:68: Why is ( cities[0] != expected )Boston was not added to Firestore: 
             got &{Boston  USA true 685000}  
             want &{Boston  USA true 685000}
FAIL
exit status 1
以下是测试:

//为什么这些结构不匹配?
log.Printf(“**2个城市[0]是类型%T,有%#v\n”,城市[0],城市[0])
log.Printf(“*****2应为类型%T有%#v\n”,应为,应为)
//当两者都是具有相同值的相同类型时,为什么比较失败?
如果城市[0]!=期望{
t、 错误f(“为什么(城市[0]!=预期)波士顿未添加到Firestore:\n获得%v\n想要%v”,预期城市[0])
}

比较指针,看起来它们指向不同的对象。要进行正确的检查,应比较对象的字段

此软件包可以帮助您执行以下操作:

例如:

请显示您的所有代码,或者至少显示一个示例,说明
预期的内容以及创建方式。
预期的
变量是
接口{}
值还是
城市
结构值?无疑是正确的:您正在比较指针值,而不是底层对象内容。您可能想要
if*cities[0]!=*此处应为
。参见操场示例。