1 回答

TA貢獻1852條經驗 獲得超7個贊
這個問題跟你想的不太一樣;該protobuf
包隨機選擇一個空格或\u00a0
在輸出錯誤時(我相信它基于二進制的哈希)。你可以在這里看到這個:
// Deliberately introduce instability into the error message string to
// discourage users from performing error string comparisons.
if detrand.Bool() {
return "proto: " // use non-breaking spaces (U+00a0)
} else {
return "proto: " // use regular spaces (U+0020)
}
所以你遇到的問題是故意的,旨在防止用戶做你正在嘗試的事情(依賴于保持不變的錯誤)。你只有在更改測試時才會看到它(我猜,不是每次更改它時)的原因是它Go
會緩存測試結果(默認情況下,測試僅在發生更改時運行)。
關于你能做些什么,我首先建議考慮這個測試是否真的有必要。軟件包作者特別指出錯誤不穩定,因此在google.golang.org/protobuf/proto
發布新版本時,以這種方式進行比較可能會中斷。
包測試通過調用(例如這里protobuf
)來解決這個問題。您不能這樣做,因為is under and, as such, not accessible。detrand.Disable()
google.golang.org/protobuf/internal/detrand
internal
如果您真的想解決這個問題,最簡單的方法可能是strings.Contains
。
- 1 回答
- 0 關注
- 625 瀏覽
添加回答
舉報