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
650
マルチクラスタの認知負荷に立ち向かう! 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
数百円から始めるRuby電子工作
tarosay
0
140
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
670
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
630
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
100
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
150
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
250
今さら聞けない .NET CLI
htkym
0
180
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
310
テーブルをDELETEした
yuzneri
0
140
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
340
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.6k
Featured
See All Featured
Accessibility Awareness
sabderemane
1
170
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
160
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
350
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
WENDY [Excerpt]
tessaabrams
11
39k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
Building Applications with DynamoDB
mza
96
7.2k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
How to build a perfect <img>
jonoalderson
1
5.9k
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