網(wǎng)站首頁 編程語言 正文
背景說明:
10 * time.Second //正常數(shù)字相乘沒錯
但是
package main import "time" func main(){ connectTimeout := 10 time.Sleep(time.Second*connectTimeout) }
這樣使用會報錯
int and time.Duration are different types. You need to convert the int to a time.Duration?
原因分析:
原因:因為類型不匹配,time.Duration類型 不能直接和 int類型相乘,需要先將變量轉(zhuǎn)換為time.Duration
解決方式:time.Duration(int變量))
解決方法:
要將整數(shù)個單位轉(zhuǎn)換為持續(xù)時間
seconds := 10 ctx, cancel := context.WithTimeout(context.Background(), time.Duration(seconds) * time.Second) //ctx, cancel := context.WithCancel(context.Background()) defer cancel()
// Common durations. There is no definition for units of Day or larger // to avoid confusion across daylight savings time zone transitions. // // To count the number of units in a Duration, divide: // second := time.Second // fmt.Print(int64(second/time.Millisecond)) // prints 1000 // // To convert an integer number of units to a Duration, multiply: // seconds := 10 // fmt.Print(time.Duration(seconds)*time.Second) // prints 10s
參考
Golang:如何將int轉(zhuǎn)換為time.duration?
參考URL: https://ask.csdn.net/questions/1037457
golang time.Duration 自定義變量報錯解決
參考文末介紹。
下面看下:golang time.Duration 自定義變量報錯解決
對于time.Duration類型,如果采用 time.Duration類型 * int變量 會報錯,而直接和數(shù)字相乘則不會出現(xiàn);
具體是為什么呢?怎么解決呢?
錯誤:Invalid operation: time.Millisecond * idcTimeOut (mismatched types Duration and int64)
原因:因為類型不匹配,time.Duration類型 不能直接和 int類型相乘,需要先將變量轉(zhuǎn)換為time.Duration
解決方式:time.Duration(int變量))
代碼如下:
?idc := getIdc() ?? ?var idcTimeOut int64 ?? ?if _, ok := IdcTimeout[idc]; ok { ?? ??? ?idcTimeOut = IdcTimeout[idc] ?? ?} else { ?? ??? ?idcTimeOut = AllTimeout ?? ?} ? ? //錯誤寫法 ? ? time.After(time.Millisecond * idcTimeOut ? ? //正確寫法 ? ? time.After(time.Millisecond * time.Duration(idcTimeOut))
原文鏈接:https://blog.csdn.net/inthat/article/details/123895596
相關(guān)推薦
- 2022-05-26 Flutter?UI實現(xiàn)側(cè)拉抽屜菜單_Android
- 2022-07-09 kettle訪問webservices接口讀取數(shù)據(jù)插入數(shù)據(jù)庫
- 2022-08-05 C語言簡明介紹常見關(guān)鍵字的用法_C 語言
- 2022-05-22 nginx共享內(nèi)存的機(jī)制詳解_nginx
- 2022-11-08 PostgreSQL長事務(wù)與失效的索引查詢淺析介紹_PostgreSQL
- 2023-03-18 Android?UI動態(tài)設(shè)置帶有Stroke漸變色背景Drawable_Android
- 2022-07-26 Python使用psutil獲取系統(tǒng)信息_python
- 2022-11-13 Python?argparse模塊實現(xiàn)解析命令行參數(shù)方法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支