Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Goと定数 DMM.go #3
Search
teru0x1
July 20, 2021
Programming
2.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Goと定数 DMM.go #3
teru0x1
July 20, 2021
More Decks by teru0x1
See All by teru0x1
開発効率と信頼性を両立する Ubieのプラットフォームエンジニアリング
teru0x1
0
610
マルチクラスタの認知負荷に立ち向かう! Ubieのプラットフォームエンジニアリング
teru0x1
4
4.9k
ブラウザの外側でWasmを使おう
teru0x1
0
400
スタブサーバ自動生成ツール 〜負荷試験をもっと楽に〜
teru0x1
0
2.1k
バッチシステムをクラウドネイティブにするために考えたこと
teru0x1
17
8.6k
クラウド環境をFargateに 移行して得た知見
teru0x1
0
1.7k
はてなインターン2020成果発表
teru0x1
0
1.2k
入門QUIC
teru0x1
0
600
【衝撃】Archlinuxをインストールした結果がヤバすぎた!
teru0x1
0
140
Other Decks in Programming
See All in Programming
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
190
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.3k
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
180
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
320
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
770
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.4k
JavaDoc 再入門
nagise
1
420
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
200
Oxlintのカスタムルールの現況
syumai
6
1.2k
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
160
Featured
See All Featured
My Coaching Mixtape
mlcsv
0
150
The agentic SEO stack - context over prompts
schlessera
0
820
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Optimizing for Happiness
mojombo
378
71k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
220
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
4 Signs Your Business is Dying
shpigford
187
22k
Designing for Performance
lara
611
70k
Designing for Timeless Needs
cassininazir
1
260
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
The Spectacular Lies of Maps
axbom
PRO
1
820
Transcript
© DMM.com Goと定数 小野 輝也 DMM.go #3 2021/07/20
© DMM.com 自己紹介 • 2021年新卒入社 • ITインフラ本部 SRE部 • 今週から配属されました
• 普段はGoでWebアプリを書いたりRust でネットワークプログラミングをする同人 誌を書いたりしています 2 小野 輝也(おの てるや) Twitter: @teru0x1
© DMM.com 今回お話しする内容 • Goにおける定数の基本 • Goにおける定数の上手な使い方 3
© DMM.com 定数 a: float64, 1 b: string, Welcome to
DMM.go c: string, Welcome to DMM.go d: int, 3 e: complex128, (2+3i) f: bool, false true: bool, false 4 • 定数になれるのはboolean, rune, integer, floating-point, complex, stringのみ • 定義を空にすると直上の式をコピーする • コンパイル時に値は決定される const ( true = 0 == 0 false = 0 != 0 ) builtin.go
© DMM.com iota • 定数の生成器 • const ( … )に含まれる定数定義の0から始まるインデックスを返す
• 同じ行に含まれるiotaは同じ値を返す • 擬似的にEnumを作る時などに利用する 5
© DMM.com 定数について話そうと思った経緯 6 ❌ invalid operation: a * time.Second
(mismatched types int and time.Duration) ✅ OK
© DMM.com Goの計算ルール • 型が一致しているもの同士が計算できる • int64とint8で四則演算することはできない • 明示的な型変換が必要 7
❌ invalid operation: a + b (mismatched types int64 and int8) ✅ OK
© DMM.com 定数 • constキーワードを用いて定義(例: const a = “hello”) •
左辺: named constant • 右辺: unnamed constant • これらはどちらも“untyped constant”に分類される • リテラルのフォーマットをもとにstringやfloatなどに分類される • ”hello”: untyped string constant • 1.23: untyped float constant • 1 + 5i: untyped complex constant • 定数には型を明示することもできる(例: const a int = 3) 8
© DMM.com untyped constant • 明示的な型の情報を持たない定数 • 「型が厳密に一致していないと四則演算できない」というルールからは外 れる •
デフォルト型を持つ 9 ❌ a, bは型の情報を持つため掛け算で きない invalid operation: a * b (mismatched types int and float64) ✅ a, bはuntyped constantなので掛け算 できる
© DMM.com デフォルト型 • untyped constantが持つ暗黙的な型 • 型が必要となった際、明示された型情報がない場合はデフォルト型が利 用される •
シンタックスにより決定される 10 結果: float64, main.MyFloat64 a: 1.5のデフォルト型(float64) b: 明示的な型(MyFloat64)
© DMM.com untyped constantsの例 11
© DMM.com untyped float • 任意の精度、桁数を持つ • 高い精度での計算が可能 • 例:
mathパッケージの定数(const.go) • 変数に代入などしてしまうとfloat64またはfloat32になる • そのため、幾らかの情報は失われてしまう • untyped float同士でできる限り計算すると高い精度を維持できる 12 math/const.go
© DMM.com 複数の型になれるuntyped • 定数値によっては様々な型になることができるものもある • 虚部が0のcomplexならfloat, intに • 小数部分が0のfloatならintに
13
© DMM.com 最初に戻ると • 左: untyped int constantとtime.Durationの積 • time.Secondはtime.Dutation
constant(typed) • time.Durationはint64のDefined Type (type Dutation int64) • 右: intとtime.Durationの積 14 ❌ invalid operation: a * time.Second (mismatched types int and time.Duration) ✅ OK
© DMM.com まとめ • Goの定数には柔軟性・利便性を支える様々な仕組みがある • iota • 定数定義の省略による式コピー •
untyped constants • 定数を定義するときにはuntypedをうまく活用し、 型の決定をなるべく遅らせるのがGood 15
© DMM.com 参考 • GoのLanguage Specification • https://golang.org/ref/spec#Constant_declarations • https://golang.org/ref/spec#Constant_expressions
• https://golang.org/ref/spec#Types • https://blog.golang.org/constants 16