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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
350
Lessons from Spec-Driven Development
simas
PRO
0
210
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
Performance Engineering for Everyone
elenatanasoiu
0
180
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
180
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
360
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.7k
New "Type" system on PicoRuby
pocke
1
970
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
Contextとはなにか
chiroruxx
1
330
Agentic UI
manfredsteyer
PRO
0
180
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
The Pragmatic Product Professional
lauravandoore
37
7.3k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.4k
How to Ace a Technical Interview
jacobian
281
24k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
250
The agentic SEO stack - context over prompts
schlessera
0
820
Become a Pro
speakerdeck
PRO
31
6k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
850
Building Applications with DynamoDB
mza
96
7.1k
Faster Mobile Websites
deanohume
310
31k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
250
How to Think Like a Performance Engineer
csswizardry
28
2.7k
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