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

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

uji
May 14, 2022

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

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


    View Slide

  2. Name

    uji

    Twitter

    @uji_rb

    Orgs


    Kyoto.go

    Profile


    View Slide

  3. Go Conference 2022 Spring

    https://gocon.jp/2022spring

    archive: https://youtu.be/GZCr-Dbi0YM?t=6205

    slide: https://t.co/3DGnnOslLG


    View Slide

  4. What is iota?


    View Slide

  5. Goにおける定数生成器

    Go's Constant generator.


    定数に連番の整数を割り当てる際に便利

    Useful for assigning sequential integers to constants.

    What is iota?


    View Slide

  6. iotaを使わない連番定数の宣言

    Declaration of sequential constants without iota.


    View Slide

  7. iotaを使った連番定数の宣言

    Declaration of sequential constants with iota.

    全て = iota で表現できる

    All can be expressed using = iota


    View Slide

  8. iotaを使った連番定数の宣言

    Declaration of sequential constants with iota.

    さらに定義を省略できる

    Definitions can be more abbreviated.


    View Slide

  9. iota Specification


    View Slide

  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


    View Slide

  11. iota Specification

    Approved = ?


    View Slide

  12. Approved = 2

    iota Specification

    index: 0

    index: 1

    index: 2


    View Slide

  13. iota Specification

    Approved = ?

    https://go.dev/play/p/ZxykC3awxTk


    View Slide

  14. iota Specification

    Approved = 2 + 2 = 4

    https://go.dev/play/p/ZxykC3awxTk

    index: 0

    index: 1

    index: 2


    View Slide

  15. Tips


    View Slide

  16. iota + 1







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

    Prevent unintentional classification of zero-valued variables

    Tips


    View Slide

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

    Name 0 constants Unknown, etc.

    Tips


    View Slide

  18. 1 << iota






    複数の状態を一つ値で表現したいときに効果的

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

    Tips


    View Slide

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

    Tips

    ビット和(OR)で

    CreateとReadが

    許可された権限を表現


    ORs can express the
    permissions allowed to
    Create and Read.


    View Slide

  20. Tips

    ビット積(AND)で権限
    チェック


    AND can be used to
    check authority.

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


    View Slide

  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.


    View Slide

  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?


    View Slide

  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


    View Slide