給定以下功能:func convertValue(contents string) (int, error) { return strconv.Atoi(contents)}當我運行以下測試時var convertValues = []struct { contents string value int}{ {"9223372036854775807", math.MaxInt64}, {"?9223372036854775808", math.MinInt64},}func TestConvertValue(t *testing.T) { for _, values := range convertValues { value, err := convertValue(values.contents) if err != nil { t.Error("Expecting", values.value, "but got error", err.Error()) } if value != values.value { t.Error("Expecting ", values.value, ", but got ", value) } }}它適用于 MaxInt64,但不適用于 MinInt64。我在 MacBookPro 上運行它,所以它在 64 位上運行。我已經用以下內容仔細檢查了這一點func TestIntSize(t *testing.T) { const PtrSize = 32 << uintptr(^uintptr(0)>>63) fmt.Println(runtime.GOOS, runtime.GOARCH) fmt.Println(strconv.IntSize, PtrSize)}它返回了darwin amd6464 64我究竟做錯了什么?
1 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
https://play.golang.org/p/FtytYJkHSc
切換到 strconv.ParseInt 并明確使用 int64 作為類型似乎有所幫助。我的 MinInt64 字符串中的破折號也有問題,這可能就在我的最后,但值得檢查并確保它是基本的 ASCII 字符,而不是某種 Unicode 破折號。
- 1 回答
- 0 關注
- 273 瀏覽
添加回答
舉報
0/150
提交
取消