第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

將接口轉換為 int64 無法按預期工作

將接口轉換為 int64 無法按預期工作

Go
函數式編程 2022-07-18 15:41:48
我正在嘗試編寫一種將紀元時間戳轉換為int64值的方法,但該方法可能會獲取多種數據類型;例如int64, int, string. 我有以下代碼:package mainimport (    "fmt")func test(t interface{}) {    tInt64, ok := t.(int64)    fmt.Println("initial value:", t)    fmt.Printf("initial type: %T\n", t)    fmt.Println("casting status:", ok)    fmt.Println("converted:", tInt64)}func main() {    t := 1606800000    tStr := "1606800000"    test(t)    test(tStr)}我希望它能夠成功地將t和tStr變量轉換為int64; 但是,結果如下:initial value: 1606800000initial type: intcasting status: falseconverted: 0initial value: 1606800000initial type: stringcasting status: falseconverted: 0我不知道它是否相關;但我使用三個版本的 golang 編譯器執(zhí)行代碼1.13:1.14和1.15. 都有相同的輸出。
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

Go 沒有要求的功能。寫一些這樣的代碼:


func test(t interface{}) (int64, error) {

    switch t := t.(type) {   // This is a type switch.

    case int64:

        return t, nil        // All done if we got an int64.

    case int:

        return int64(t), nil // This uses a conversion from int to int64

    case string:

        return strconv.ParseInt(t, 10, 64)

    default:

        return 0, fmt.Errorf("type %T not supported", t)

    }

}


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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