Slide 1

Slide 1 text

5分で完全理解するGoのiota
 Fully understand Go's iota in 5 minutes.
 
 uji


Slide 2

Slide 2 text

Name
 uji
 Twitter
 @uji_rb
 Orgs
 
 Kyoto.go
 Profile


Slide 3

Slide 3 text

Go Conference 2022 Spring
 https://gocon.jp/2022spring
 archive: https://youtu.be/GZCr-Dbi0YM?t=6205
 slide: https://t.co/3DGnnOslLG


Slide 4

Slide 4 text

What is iota?


Slide 5

Slide 5 text

Goにおける定数生成器
 Go's Constant generator.
 
 定数に連番の整数を割り当てる際に便利
 Useful for assigning sequential integers to constants.
 What is iota?


Slide 6

Slide 6 text

iotaを使わない連番定数の宣言
 Declaration of sequential constants without iota.


Slide 7

Slide 7 text

iotaを使った連番定数の宣言
 Declaration of sequential constants with iota.
 全て = iota で表現できる
 All can be expressed using = iota


Slide 8

Slide 8 text

iotaを使った連番定数の宣言
 Declaration of sequential constants with iota.
 さらに定義を省略できる
 Definitions can be more abbreviated.


Slide 9

Slide 9 text

iota Specification


Slide 10

Slide 10 text

● 定義は定数, コンパイル時に値が決まる
 Definition is constant, value determined at compile time.
 ● ConstDecl中のConstSpecのindex値が入る
 The index value of ConstSpec in ConstDecl is entered.
 
 
 
 
 カウントアップしているわけではない
 iota Specification
 ConstDecl
 ConstSpec


Slide 11

Slide 11 text

iota Specification
 Approved = ?


Slide 12

Slide 12 text

Approved = 2
 iota Specification
 index: 0
 index: 1
 index: 2


Slide 13

Slide 13 text

iota Specification
 Approved = ?
 https://go.dev/play/p/ZxykC3awxTk


Slide 14

Slide 14 text

iota Specification
 Approved = 2 + 2 = 4
 https://go.dev/play/p/ZxykC3awxTk
 index: 0
 index: 1
 index: 2


Slide 15

Slide 15 text

Tips


Slide 16

Slide 16 text

iota + 1
 
 
 
 
 
 
 ゼロ値の変数が意図せず分類されてしまうのを防ぐ
 Prevent unintentional classification of zero-valued variables
 Tips


Slide 17

Slide 17 text

0の定数をUnknownなどの命名にするやり方もある
 Name 0 constants Unknown, etc.
 Tips


Slide 18

Slide 18 text

1 << iota
 
 
 
 
 
 複数の状態を一つ値で表現したいときに効果的
 This is effective when you want to express multiple states with a single value.
 Tips


Slide 19

Slide 19 text

https://go.dev/play/p/3Aub3AACmco
 Tips
 ビット和(OR)で
 CreateとReadが
 許可された権限を表現
 
 ORs can express the permissions allowed to Create and Read.


Slide 20

Slide 20 text

Tips
 ビット積(AND)で権限 チェック
 
 AND can be used to check authority.
 https://go.dev/play/p/3Aub3AACmco


Slide 21

Slide 21 text

「数値に意味がある」定数 Constants with meaningful numerical values
 e.g.
 ・Exit Code
 ・DBに定義された値 Value defined in DB
 コードの差分に数値の変更が表れないため意図しない数値の変更に 気付きにくい
 Difficult to notice unintended numerical changes because numerical changes are not represented in the code differences.
 Iotaを使わない方が良い場面
 Situations where iota should not be used.


Slide 22

Slide 22 text

Goチームは一般的な列挙型は解決したい課題に
 対して仕様が複雑すぎると考えている
 The Go team believes that general enums are too complex a specification for the problem they want to solve.
 
 Go2に向けて今も議論されている
 Still being discussed for Go2.
 https://github.com/golang/go/issues/28987
 Why not Enum?


Slide 23

Slide 23 text

● iotaはGoの連番定数生成の仕組み
 iota is Go's sequential constant generation mechanism
 ● iotaはconst宣言のindex値として扱われる
 iota is treated as an index value of const declaration
 ● iota + 1でゼロ値による意図しない挙動を防げる
 `iota +1` to prevent unintended behavior due to zero values.
 ● 1 << iotaで単一の値で複雑な状態も表現できる
 Complex states can be represented with a single value in `1 << iota`
 
 Summary