在Go语言中,是有uint64字面量的。

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

Are there uint64 literals in Go?

问题

我正在查看Go语言中的数字类型。我想要使用uint64字面量,这在Go语言中是否可行?

以下是我想要使用uint64字面量的示例代码:

for i := uint64(2); i <= k; i += 1 { // 我希望i是一个uint64类型
    ...
}
英文:

I'm looking at the numeric types in Go. I want to use uint64 literals. Is this possible in Go?

Here's an example of how I'd like to use uint64 literals:

for i := 2; i &lt;= k; i += 1 { // I want i to be a uint64
    ...
}

答案1

得分: 18

你可以将整数字面量直接转换为uint64类型。

for i := uint64(1); i <= k; i++ {
    // 做一些操作
}

或者你可以在for循环外部初始化i,但这样它的作用域就比循环本身要大。

var i uint64
for i = 1; i <= k; i++ {
    // 注意使用`=`而不是`:=`
}
// i仍然存在,并且现在的值是k+1
英文:

you can just cast your integer literal to uint64.

for i := uint64(1); i &lt;= k; i++ {
    // do something
}

Alternatively you could initialize i outside of the for loop, but then it's scoped larger than the loop itself.

var i uint64
for i = 1; i &lt;= k; i++ {
    // note the `=` instead of the `:=`
}
// i still exists and is now k+1

答案2

得分: 3

让我们来看一下常量的规范:https://go.dev/ref/spec#Constants。他们说:

常量可以通过常量声明或转换显式地给定类型,或者在变量声明、赋值或表达式的操作数中隐式地给定类型。

并且:

无类型常量具有默认类型,该类型是在需要类型化值的上下文中隐式转换为常量的类型,例如,在没有显式类型的短变量声明中,如i := 0。无类型常量的默认类型分别为boolruneintfloat64complex128string,具体取决于它是布尔、符文、整数、浮点、复数还是字符串常量。

根据这些说明,并结合你的代码上下文,初始化一个不在默认类型列表中的变量(如uint64)的最佳方法是进行转换:

for i := uint64(2); i <= k; i++ {
  ...
}
英文:

Let's take a look at the specification for constant: https://go.dev/ref/spec#Constants. This is what they said:

> A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression.

And:

> An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.

Based on these statements and in the context of your code, the best way to initialize a variable that is not in the default type list like uint64 it to convert:

for i := uint64(2); i &lt;= k; i++ {
  ...
}

答案3

得分: 0

你必须明确声明变量的类型。int字面量将是int类型,例如var i uint64。在你的示例中,你还需要更改赋值部分,像这样:

var i uint64
for i = 2; i <= k; i += 1 { // 我希望i是uint64类型
    ...
}
英文:

You have to explicitly declare your variables as that type. The int literal will be of type int https://play.golang.org/p/OgaZzmpLfB something like var i uint64 is required. In your example you'd have to change your assignment as well so something like this;

var i uint64
for i = 2; i &lt;= k; i += 1 { // I want i to be a uint64
    ...
}

huangapple
  • 本文由 发表于 2015年12月20日 06:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/34376141.html
匿名

发表评论

匿名网友

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

确定