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

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

單值上下文中的多值

單值上下文中的多值

Go
元芳怎么了 2021-09-27 21:05:06
我目前正在嘗試 Go 并且遇到了上述錯誤消息。看看接口,它對 float64 的實現和測試。界面:package intervalimport (    "errors"    "fmt"    "math")type Interval interface {    Intersect(Y Interval) (Interval, error) // Intersection of X and Y, error 'nil' when empty}type floatInterval struct {    a, b float64}func (fi floatInterval) Intersect(Y Interval) (Interval, error) {    tmp := Y.(floatInterval)    a_new, b_new := math.Max(fi.a, tmp.a), math.Min(fi.b, tmp.b)    result := floatInterval{a_new, b_new}    if result.Length() == 0 {        return result, errors.New("Empty interval")    } else {        return result, nil    }}測試:func intersect_test(t *testing.T, c testTuple) {    got, _ := c.iv1.Intersect(c.iv2).(floatInterval)    if (c.intersectWant.a != got.a) || (c.intersectWant.b != got.b) {        t.Errorf("Expected: [%f, %f] \t Got: [%f, %f]", c.intersectWant.a, c.intersectWant.b, got.a, got.b)    }}錯誤發生在測試函數的第二行。我知道 intersect 返回兩個值:間隔和錯誤值。但由于我將兩者都分配給我,got, _ := c.iv1.Intersect(c.iv2).(floatInterval)我認為我是安全的。got, err := ...順便我也試過。這是由于我正在做的類型轉換.(floatInterval)嗎?
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

這是因為類型斷言只需要一個值。


改為這樣做:


gotInterval, _ := c.iv1.Intersect(c.iv2)

got := gotInterval.(floatInterval)


查看完整回答
反對 回復 2021-09-27
  • 1 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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