GolangNote

Golang笔记

mismatched types time.Duration and int

Permalink

这是使用一个整数作为 time.Sleep 函数的参数时遇到的一个错误提示,首先要把int 转为time.Duration 形式。

mismatched types time.Duration and int

比如:

Go: mismatched types time.Duration and int
1
2
3
4
5
t := 5
time.Sleep(time.Second * 5) // ok 正常
time.Sleep(time.Second * t)  // error 出现标题错误提示
time.Sleep(time.Second * time.Duration(t)) // ok 应该这样转

强制把 int 标为 time.Duration 类型可以解决这个问题

本文网址: https://golangnote.com/topic/47.html 转摘请注明来源

Write a Comment to "mismatched types time.Duration and int"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms