亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這些看似相同的 Golang 結構不相等?

為什么這些看似相同的 Golang 結構不相等?

Go
一只甜甜圈 2022-05-10 13:33:22
我正在嘗試學習 Golang 測試。我正在比較 2 個看似相同的結構,但它們并不相等。出了什么問題?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}FAILexit status 1這是測試:        // why don't these structs match?        log.Printf("****2 cities[0] is type %T has %#v \n", cities[0], cities[0])        log.Printf("****2 expected  is type %T has %#v \n", expected, expected)        // why is this comparison failing when both are the same type with the same values?        if cities[0] != expected {            t.Errorf("Why is ( cities[0] != expected )Boston was not added to Firestore: \n got %v  \n want %v", cities[0], expected)        }
查看完整描述

1 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

您比較指針,看起來它們指向不同的對象。為了正確檢查,您應該比較對象的字段。

這個包可以幫助你做到這一點:reflect.DeepEqual()testify.EqualValues(),cmp.Equal()

示例: https: //play.golang.org/p/09BYFeYj5xx


查看完整回答
反對 回復 2022-05-10
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號