Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of multiplying numbers and time in time package of Go language

Shulou Source: shulou.com Published: 2022-06-01 01:42:54 10月02日 Update

Today, I would like to share with you how to solve the problem of multiplication of numbers and time in the time package of Go language. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Background note:

10 * time.Second / / multiply normal numbers, yes.

But

Package mainimport "time" func main () {connectTimeout: = 10 time.Sleep (time.Second*connectTimeout)}

If you use it in this way, you will report an error.

Int and time.Duration are different types. You need to convert the int to a time.Duration

Cause analysis:

Reason: because the type does not match, the time.Duration type cannot be directly multiplied by the int type. You need to convert the variable to time.Duration first.

Solution: time.Duration (int variable))

Solution:

To convert an entire number of units to duration

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 time.Duration / To convert an integer number of units to a Duration, multiply:// seconds: = 10 time.Duration / fmt.Print (time.Duration (seconds) * time.Second) / / prints 10s reference

Golang: how do I convert int to time.duration?

Reference URL: https://ask.csdn.net/questions/1037457

Error Resolution of golang time.Duration Custom variable

Refer to the introduction at the end of the article.

Let's take a look at this: golang time.Duration custom variable error resolution

For the time.Duration type, if you use the time.Duration type * int variable, you will get an error, but multiplying the number directly will not occur.

Why exactly? How to solve it?

Error: Invalid operation: time.Millisecond * idcTimeOut (mismatched types Duration and int64)

Reason: because the type does not match, the time.Duration type cannot be directly multiplied by the int type. You need to convert the variable to time.Duration first.

Solution: time.Duration (int variable))

The code is as follows:

Idc: = getIdc () var idcTimeOut int64 if _, ok: = IdcTimeout [idc] Ok {idcTimeOut = IdcTimeout [idc]} else {idcTimeOut = AllTimeout} / / misspelled time.After (time.Millisecond * idcTimeOut / / correct time.After (time.Millisecond * time.Duration (idcTimeOut)) is all the contents of the article "how to solve the problem of multiplying numbers and time in time packets in Go language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Type variable number knowledge article reference time reason language problem content writing way error will report first different very big code unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology MySQL Apple Huawei