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 full-size slide

  2. Name

    uji

    Twitter

    @uji_rb

    Orgs


    Kyoto.go

    Profile


    View full-size slide

  3. Go Conference 2022 Spring

    https://gocon.jp/2022spring

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

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


    View full-size slide

  4. What is iota?


    View full-size slide

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

    Go's Constant generator.


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

    Useful for assigning sequential integers to constants.

    What is iota?


    View full-size slide

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

    Declaration of sequential constants without iota.


    View full-size slide

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

    Declaration of sequential constants with iota.

    全て = iota で表現できる

    All can be expressed using = iota


    View full-size slide

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

    Declaration of sequential constants with iota.

    さらに定義を省略できる

    Definitions can be more abbreviated.


    View full-size slide

  9. iota Specification


    View full-size 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 full-size slide

  11. iota Specification

    Approved = ?


    View full-size slide

  12. Approved = 2

    iota Specification

    index: 0

    index: 1

    index: 2


    View full-size slide

  13. iota Specification

    Approved = ?

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


    View full-size slide

  14. iota Specification

    Approved = 2 + 2 = 4

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

    index: 0

    index: 1

    index: 2


    View full-size slide

  15. iota + 1







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

    Prevent unintentional classification of zero-valued variables

    Tips


    View full-size slide

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

    Name 0 constants Unknown, etc.

    Tips


    View full-size slide

  17. 1 << iota






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

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

    Tips


    View full-size slide

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

    Tips

    ビット和(OR)で

    CreateとReadが

    許可された権限を表現


    ORs can express the
    permissions allowed to
    Create and Read.


    View full-size slide

  19. Tips

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


    AND can be used to
    check authority.

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


    View full-size slide

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

  21. 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 full-size slide

  22. ● 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 full-size slide