Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5分で完全理解するGoのiota

9c23bec5e8b0aa1d9458d40800987347?s=47 uji
May 14, 2022

 5分で完全理解するGoのiota

9c23bec5e8b0aa1d9458d40800987347?s=128

uji

May 14, 2022
Tweet

More Decks by uji

Other Decks in Technology

Transcript

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


  2. Name
 uji
 Twitter
 @uji_rb
 Orgs
 
 Kyoto.go
 Profile


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


  4. What is iota?


  5. Goにおける定数生成器
 Go's Constant generator.
 
 定数に連番の整数を割り当てる際に便利
 Useful for assigning sequential

    integers to constants.
 What is iota?

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


  7. iotaを使った連番定数の宣言
 Declaration of sequential constants with iota.
 全て = iota

    で表現できる
 All can be expressed using = iota

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

    be more abbreviated.

  9. iota Specification


  10. • 定義は定数, コンパイル時に値が決まる
 Definition is constant, value determined at compile

    time.
 • ConstDecl中のConstSpecのindex値が入る
 The index value of ConstSpec in ConstDecl is entered.
 
 
 
 
 カウントアップしているわけではない
 iota Specification
 ConstDecl
 ConstSpec

  11. iota Specification
 Approved = ?


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

    2

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


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


    index: 0
 index: 1
 index: 2

  15. Tips


  16. iota + 1
 
 
 
 
 
 
 ゼロ値の変数が意図せず分類されてしまうのを防ぐ


    Prevent unintentional classification of zero-valued variables
 Tips

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


  18. 1 << iota
 
 
 
 
 
 複数の状態を一つ値で表現したいときに効果的
 This

    is effective when you want to express multiple states with a single value.
 Tips

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

    permissions allowed to Create and Read.

  20. Tips
 ビット積(AND)で権限 チェック
 
 AND can be used to check

    authority.
 https://go.dev/play/p/3Aub3AACmco

  21. 「数値に意味がある」定数 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.

  22. 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?

  23. • 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