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/detrandinternal
如果您真的想解决这个问题,最简单的方法可能是strings.Contains。
- 1 回答
- 0 关注
- 829 浏览
添加回答
举报
