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
650
マルチクラスタの認知負荷に立ち向かう! Ubieのプラットフォームエンジニアリング
teru0x1
4
5k
ブラウザの外側でWasmを使おう
teru0x1
0
400
スタブサーバ自動生成ツール 〜負荷試験をもっと楽に〜
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
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
200
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
0
150
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
960
霧の中の代数的エフェクト
funnyycat
1
460
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
210
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
200
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.8k
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
390
仕様駆動開発の消費期限
watany
6
1.2k
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
230
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.8k
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
130
Featured
See All Featured
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
240
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
We Have a Design System, Now What?
morganepeng
55
8.2k
Scaling GitHub
holman
464
140k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Believing is Seeing
oripsolob
1
180
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
The SEO identity crisis: Don't let AI make you average
varn
0
520
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
160
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.5k
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