Movatterモバイル変換


[0]ホーム

URL:


Go Wiki: Iota

Summary

Go’siota identifier is used inconst declarations to simplify definitions of incrementing numbers. Because it can be used in expressions, it provides a generality beyond that of simple enumerations.

The value of iota is reset to 0 whenever the reserved wordconst appears in the source (i.e. each const block) and incremented by one after eachConstSpec e.g. each Line. This can be combined with the constant shorthand (leaving out everything after the constant name) to very concisely define related constants.

Iota:https://go.dev/ref/spec#Iota

Constant declarations:https://go.dev/ref/spec#Constant_declarations

Examples

The official spec has two great examples:

https://go.dev/ref/spec#Iota

Here’s one from Effective Go:

type ByteSize float64const (    _           = iota // ignore first value by assigning to blank identifier    KB ByteSize = 1 << (10 * iota)    MB    GB    TB    PB    EB    ZB    YB)

Weekday enum example - How iota is calculated - FromLearn Go Programming Blog:

How iota works

Articles


This content is part of theGo Wiki.

go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2026 Movatter.jp