Golang:如何将int转换为time.Duration进行计算

huangapple go评论95阅读模式
英文:

Golang: How to convert int to calculate into time.duration

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对Go语言还不熟悉,目前对编译器为什么不接受特定的代码行感到困惑。

我有一个用于创建拨号超时的工作示例:

conn, err := grpc.Dial(*connAddress,
  grpc.WithInsecure(),
  grpc.WithBlock(),                  // will block till the connection is available
  grpc.WithTimeout(100*time.Second)) // times out after 100 seconds

现在,硬编码的100不太好,所以我想通过命令行标志将其作为一个变量,像这样:

connTimeout := flag.Int64("connection-timeout", 100, "give the timeout for dialing connection x")
...
conn, err := grpc.Dial(*connAddress,
  grpc.WithInsecure(),
  grpc.WithBlock(),
  grpc.WithTimeout(*connTimeout*time.Second))

然而,这会导致以下编译错误:

mismatched types int64 and time.Duration

所以显然我不能直接使用int64类型的标志变量来计算持续时间,但是可以使用数字?

最终,我找到了一个解决方案,通过首先创建与time.Duration相关的变量来使用标志变量:

var timeoutduration = time.Duration(*distributorTimeout) * time.Second
// create distributor connection
conn, err := grpc.Dial(*connAddress,
  grpc.WithBlock(),
  grpc.WithTimeout(timeoutduration))

上述代码似乎按预期工作:拨号尝试的时间与命令行参数给定的时间长度相同(默认为100秒),然后抛出无法建立连接的消息。很好。

然而,为什么直接使用int64类型的标志变量无法计算time.Duration呢?换句话说,对于Go语言来说,数字100和包含int64的变量之间有什么区别呢?

英文:

I'm new to GoLang and at the moment at a loss why the compiler does not accept a particular line of code.

I've got this working example for creating a timeout when dialing:

    conn, err := grpc.Dial(*connAddress,
	  grpc.WithInsecure(),
	  grpc.WithBlock(),                  // will block till the connection is available
	  grpc.WithTimeout(100*time.Second)) // times out after 100 seconds

Now the hardcoded 100 there is not nice, so I would like to make that a commandline variable via a flag, like so:

    connTimeout := flag.Int64("connection-timeout", 100, "give the timeout for dialing connection x")
...
    conn, err := grpc.Dial(*connAddress,
	  grpc.WithInsecure(),
	  grpc.WithBlock(),
	  grpc.WithTimeout(*connTimeout*time.Second)) 

However this gives the following compilation error:

> mismatched types int64 and time.Duration

So apparently I can't use the int64 flag variable directly to calculate a duration but a number can?

Eventually I found a solution for using the flag variable by creating a variable related to time.Duration first:

var timeoutduration = time.Duration(*distributorTimeout) * time.Second
	// create distributor connection
	conn, err := grpc.Dial(*connAddress,
		grpc.WithBlock(),
		grpc.WithTimeout(timeoutduration)) 

The above seems to work as expected: the dialing is tried for as long as the commandline parameter given (default 100) and than throws a message that the connection could not be made. Good.

However: How come directly using the int64 flag-variable is not possible for calculating a time.Duration, in other words what is the difference for golang between the number 100 and the variable that contains the int64?

答案1

得分: 2

grpc.WithTimeout(100*time.Second)grpc.WithTimeout(*connTimeout*time.Second) 之所以不起作用是因为赋值规则的原因:

后者表达式不满足任何规则,而前者满足以下规则之一:

  • x 是一个可以用类型 T 的值表示的无类型常量。

规则

英文:

grpc.WithTimeout(100*time.Second) and grpc.WithTimeout(*connTimeout*time.Second) does not because of assignability rules:

the latter expression satisfies none of those, and the former satisfies the

  • x is an untyped constant representable by a value of type T.

rule

huangapple
  • 本文由 发表于 2017年5月10日 20:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/43892039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定