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
680
マルチクラスタの認知負荷に立ち向かう! Ubieのプラットフォームエンジニアリング
teru0x1
4
5k
ブラウザの外側でWasmを使おう
teru0x1
0
410
スタブサーバ自動生成ツール 〜負荷試験をもっと楽に〜
teru0x1
0
2.1k
バッチシステムをクラウドネイティブにするために考えたこと
teru0x1
17
8.7k
クラウド環境をFargateに 移行して得た知見
teru0x1
0
1.7k
はてなインターン2020成果発表
teru0x1
0
1.2k
入門QUIC
teru0x1
0
610
【衝撃】Archlinuxをインストールした結果がヤバすぎた!
teru0x1
0
140
Other Decks in Programming
See All in Programming
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
200
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
110
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
570
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
220
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
140
高専キャリア LT 発表内容
crysta1221
6
5.4k
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
110
Loosening the Reins: Go Generics Get More Flexible
kuro_kurorrr
0
360
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
1
250
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.9k
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
300
Claude Codeを組織的に動かして月400PRを実現した話
happy_ryo
0
230
Featured
See All Featured
Building the Perfect Custom Keyboard
takai
2
860
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
430
Amusing Abliteration
ianozsvald
1
280
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
260
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
500
For a Future-Friendly Web
brad_frost
183
10k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
320
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.1k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
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