golang iota_iota在golang中创建有效常数

golang iota

Some concepts have names, and sometimes we care about those names, even (or especially) in our code.

有些概念有名称,有时我们会在代码中甚至(或尤其是)关心这些名称。

At other times, we only care to distinguish one thing from the other. There are times when there’s no inherently meaningful value for a thing. For example, if we’re storing products in a database table we probably don’t want to store their category as a string. We don’t care how the categories are named, and besides, marketing changes the names all the time.

在其他时候,我们只在乎将一件事与另一件事区分开。 有时候,事物本身并没有有意义的价值。 例如,如果我们将产品存储在数据库表中,则可能不想将其类别存储为字符串。 我们不在乎类别的命名方式,此外,营销始终会更改名称。

We care only that they’re distinct from each other.

我们只关心它们彼此不同。

const (
CategoryBooks = 0
CategoryHealth = 1
CategoryClothing = 2
)

Instead of 0, 1, and 2 we could have chosen 17, 43, and 61. The values are arbitrary.Constants are important but they can be hard to reason about and difficult to maintain.

我们可以选择17、43和61,而不是0、1和2。这些值是任意的。常量很重要,但它们可能难以推理且难以维护。

iota (iota)

iota makes it easy to declare sequentially growing numeric constants in Go:

iota可以轻松地在Go中声明顺序增长的数字常量:

const (
Low = iota
Medium
High
)
fmt.Printf("Low: %d\nMedium: %d\nHigh: %d\n", Low, Medium, High)

iota sets the value of Low to 0 and instructs the compiler that the following constants have increasing numeric values.

iotaLow的值设置为0,并指示以下常量常量的数值不断增加。

Low: 0
Medium: 1
High: 2

与iota合作 (Operations with iota)

It’s possible to make operation when using iotas

使用iotas时可以进行操作

package mainimport "fmt"const (
_ = iota
a = iota + 10
b int = iota * 10
c float64 = iota + 5.1
)func main() {
fmt.Printf("%v — %T\n", a, a)
fmt.Printf("%v — %T\n", b, b)
fmt.Printf("%v — %T\n", c, c)
}Output=>
11 — int
20 — int
8.1 — float64

重置IOTA增量 (Resetting iota increment)

Every time a keyword Const appears in the code, iota’s increment is reset.

每次在代码中出现关键字Const时,iota的增量都会重置。

package main
import "fmt"
const (
a = iota
b int = iota
)const (
c = iota
)
func main() {
fmt.Printf("%v — %T\n", a, a)
fmt.Printf("%v — %T\n", b, b) fmt.Printf("%v — %T\n", c, c)
}Output=>0 — int
1 — int0 — int

使用iota创建位掩码值 (Creating bitmask values with iota)

Iota can be very useful when creating a bitmask. For instance, to represent the state of a network connection which may be secure, authenticated, and/or ready, we might create a bitmask like the following:

创建位掩码时,IOTA可能非常有用。 例如,为了表示网络连接的状态(可以是安全的,已验证的和/或准备好的),我们可以创建如下的位掩码:

const (
Secure = 1 << iota // 0b001 Authn // 0b010 Ready // 0b100)// 0b011: Connection is secure and authenticated, but not yet Ready
ConnState := Secure | Authn
fmt.Printf(`Secure: 0x%x (0b%03b)
Authn: 0x%x (0b%03b)
ConnState: 0x%x (0b%03b)
`, Secure, Secure, Authn, Authn, ConnState, ConnState)Output =>
Secure: 0x1 (0b001)
Authn: 0x2 (0b010)
ConnState: 0x3 (0b011)

跳过值 (Skipping values)

The value of iota is still incremented for every entry in a constant list even if iota is not used:

即使未使用iota,对于常量列表中的每个条目, iota的值仍会增加:

const ( // iota is reset to 0	a = 1 << iota // a == 1	b = 1 << iota // b == 2	c = 3         // c == 3  (iota is not used but still  
incremented)
d = 1 << iota // d == 8)
fmt.Printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, d)Output=>
a: 1, b: 2, c: 3, d: 8

It will also be incremented even if no constant is created at all, meaning the empty identifier can be used to skip values entirely:

即使根本不创建任何常量,它也会增加,这意味着空标识符可用于完全跳过值:

const (
a = iota // a = 0 _ // iota is incremented to 1 b // b = 2)
fmt.Printf("a: %d, b: %d\n", a, b)Output=>a: 0, b: 2

在表达式列表中使用iota (Using iota in an expression list)

Because iota is incremented after each ConstSpec, values within the same expression list will have the same value for iota:

因为iota在每个ConstSpec之后ConstSpec ,所以相同表达式列表中的值将具有与iota相同的值:

const (
bit0, mask0 = 1 << iota, 1<<iota - 1 //bit0 == 1, mask0 == 0 bit1, mask1 //bit1 == 2, mask1 == 1 _, _ //skips iota == 2 bit3, mask3 //bit3 == 8, mask3 == 7)
fmt.Printf("bit0: %d, mask0: 0x%x\n", bit0, mask0)
fmt.Printf("bit3: %d, mask3: 0x%x\n", bit3, mask3)Output=>bit0: 1, mask0: 0x0
bit3: 8, mask3: 0x7

在表达式中使用iota (Using iota in an expression)

iota can be used in expressions, so it can also be used to assign values other than simple incrementing integers starting from zero. To create constants for SI units, use this example from Effective Go:

iota可以在表达式中使用,因此它也可以用于赋值,而不是从零开始的简单递增整数。 要为SI单位创建常量,请使用Effective Go中的以下示例:

type ByteSize int
const (
_ = iota //ignore first value by assigning to
//blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
)
fmt.Printf("KB: %d\n", KB)Output=>KB: 1024

模拟枚举 (Emulating enums)

Go doesn’t have a syntax for enumerations, but you can emulate it with constants and a naming scheme.

Go没有枚举的语法,但是您可以使用常量和命名方案来模拟它。

Consider a C++ enum:

考虑一个C ++枚举:

enum {
tagHtml,
tagBody,
taDiv
};

In Go you can do it as:

在Go中,您可以执行以下操作:

const (
tagBody = iota
tagDiv
tagHr
)

This declares 3 untyped, numeric constants. iota starts numbering of constants. tagBody has value of 0, tagDiv a value of 1 etc. This is convenient way to assign unique numeric values.

这将声明3个无类型的数字常量。 iota开始为常数编号。 tagBody值为0tagDiv的值为1等等,这是分配唯一数值的便捷方法。

If the values matter, you can assign them explicitly:

如果值很重要,则可以显式分配它们:

const (
one = 1
five = 5
seven = 7
)

增加类型安全性 (Adding type safety)

In the above example tagBody etc. is an untyped constant so it can be assigned to any numeric type.

在上面的示例中, tagBody等是未类型化的常量,因此可以将其分配给任何数字类型。

We can define a unique type for our enum:

我们可以为我们的枚举定义一个唯一的类型:

type HTMLTag intconst (
tagBody HTMLTag = iota
tagDiv
tagHr
)

结论 (Conclusion)

I am leaving you with this, to build upon and get hands on experience.

我将为此留给您,以建立并获得经验。

That’s it! I hope this short post about Go iota was. useful! Of course, all comments are welcome.

而已! 我希望这篇关于Go iota的短文是。 有用! 当然,欢迎所有评论。

翻译自: https://medium.com/@balagetme/iota-create-effective-constants-in-golang-b399f94aac31

golang iota

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值