Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report