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言語仕様 / Go Specification Untyped Constants
Search
DQNEO
April 15, 2021
Programming
1
1.2k
入門Go言語仕様 / Go Specification Untyped Constants
DQNEO
April 15, 2021
Tweet
Share
More Decks by DQNEO
See All by DQNEO
英和辞書付きGo言語仕様書 / Word Wise Go Spec
dqneo
1
480
Go言語低レイヤー入門 Hello world が 画面に表示されるまで / Introduction to low level programming in Go
dqneo
6
1.5k
入門Go言語仕様 Underlying Type / Go Language Underlying Type
dqneo
9
4.8k
How to write a self hosted Go compiler from scratch (Gophercon 2020)
dqneo
3
1.5k
もっと気軽にOSSに Pull Requestを出そう!/ Let's make a PR to OSS more easily
dqneo
6
8.2k
Goコンパイラをゼロから作ってセルフホスト達成するまで / How I wrote a self hosted Go compiler from scratch
dqneo
15
14k
コンパイラをつくってみよう / How to make a compiler
dqneo
9
11k
コンパイラ作りの魅力を語る / Making compilers is fun
dqneo
10
8.4k
Goのmapとheapを自作してみた / How to create your own map and heap in Go
dqneo
0
3.1k
Other Decks in Programming
See All in Programming
2025.2.14_Developers Summit 2025_登壇資料
0101unite
0
140
コードを読んで理解するko build
bells17
1
100
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
6
2.2k
PRレビューのお供にDanger
stoticdev
1
220
CI改善もDatadogとともに
taumu
0
180
Kotlinの開発でも AIをいい感じに使いたい / Making the Most of AI in Kotlin Development
kohii00
3
240
Go 1.24でジェネリックになった型エイリアスの紹介
syumai
2
260
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
870
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
150
CSS Linter による Baseline サポートの仕組み
ryo_manba
1
140
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
950
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
170
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
67
4.6k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.3k
What's in a price? How to price your products and services
michaelherold
244
12k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
10
500
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Agile that works and the tools we love
rasmusluckow
328
21k
Statistics for Hackers
jakevdp
797
220k
The Language of Interfaces
destraynor
156
24k
Transcript
入門Go言語仕様輪読会 Untyped Constants @DQNEO 2021-04-15
自己紹介 • @DQNEO (ドキュネオ) • Goコンパイラ babygo の作者 ◦ https://github.com/DQNEO/babygo
• 公式Goコンパイラ、言語仕様書コントリビュート歴有り
問題: 次の値の型は何でしょうか? 123 "hello" true
答え: なし 123 "hello" true ← 型なし ← 型なし ←
型なし
問題: 次の2文は何が違うでしょうか? const x = 123 var x = 123
注: const / var 違いの他に
答え: 型が違う const x = 123 var x = 123
var x int = 123 両辺とも型なし 両辺とも int =
型なし?
Go言語の定数は • 型あり • 型なし のどちらか
これらは全部型なし定数 • 123 • 1.1 • "hello" • ‘a’ •
true • 1.0+3.0i
型なし定数を作ることも可能 const THREE = 3 const HELLO = "hello" 左辺で型を省き、右辺で型なし定数を使う
型なし定数のおもしろ性質(1) _人人人人人人_ > 型がない <  ̄Y^Y^Y^Y^Y^Y^ ̄
型がないのでユーザ定義型に代入可能 type MyBool bool var b MyBool = true もし
true が bool 型だったら、代入できないはず (代入可能性の解説はこちら ) https://play.golang.org/p/63VHrn7XQVY
const HELLO = "hello" type MyString string var s MyString
= HELLO もし HELLO が string型だったら、代入できない 型がないのでユーザ定義型に代入可能 (代入可能性の解説はこちら )
型がないのでいろんな型に代入可能 var y float32 = 97 var x int64 =
'a' var z byte = 97.0 (※ ただし “representable” な場合に限る) どれも 右辺は 「左辺の型の97」扱い
型なし定数のおもしろ性質(2) 「型を隠し持っている」
型なし定数にも種類がある リテラル 種類 1 integer constant 1.0 floating-point constant 'a'
rune constant "hello" string constant true boolean constant
種類に応じて default type がある リテラル 種類 default type 1 integer
constant int 1.0 floating-point constant float64 'a' rune constant rune(=int32) "hello" string constant string true boolean constant bool
var x = 1.0 型が必要です。 あなたの型を教えてください float64 です 聞かれたときだけ答える default
type とは、 「型を要求されたとき」に使われる型
型を要求される文脈の例 • var x = 123 • y := 1.0
• var ifc interface{} = 1.0 • fmt.Printf("%v", 1.0)
• var x uint8 = 1.0 私は uint8型です。 私に従ってください わかりました
default typeは、必ずしも使われるわけでは ない uint8として振る舞う default typeは使われない
型なし定数のおもしろ性質(3) 「すごい数もOK」
超巨大数を定義できる const HUGE = 92233720368547758070 ↑ int64の最大値より大きい
定義できるが表示できない const HUGE = 92233720368547758070 fmt.Println(HUGE) _人人人人人人_ > コンパイルエラー <  ̄Y^Y^Y^Y^Y^Y^ ̄ constant
92233720368547758070 overflows int64
int型変数に代入できない const HUGE = 92233720368547758070 var x = HUGE constant
92233720368547758070 overflows int64 _人人人人人人_ > コンパイルエラー <  ̄Y^Y^Y^Y^Y^Y^ ̄
Go言語氏 「default type があるとは言ったが、 default type に収まるとは言ってない」
float型変数には代入できる const HUGE = 92233720368547758070 var x float64 = HUGE
fmt.Printf("%v\n", x) => 9.223372036854776e+19 精度は落ちる (当然)
巨大数同士の定数計算ができる const HUGE = 92233720368547758070 const X_HUGE = 922337203685477580700 var
x uint8 = X_HUGE / HUGE 計算結果がuint8 に収まるのでOK => 10
超細かい小数を定義できる const ROOT2 = 1.41421356237309504880168872420969807856 967187537694807317667974 float64 の精度を超えている
二乗すると 2 になる const ROOT2 = 1.41421356237309504880168872420969807856 967187537694807317667974 fmt.Println(ROOT2 *
ROOT2) // => 2 _人人人人人人_ > 精度高すぎ <  ̄Y^Y^Y^Y^Y^Y^ ̄
変数を経由すると精度が落ちる const ROOT2 = 1.41421356237309504880168872420969807856 967187537694807317667974 f := ROOT2 //
ここで float64に詰め替えられる fmt.Println(f * f) // => 2.0000000000000004 https://play.golang.org/p/JMtw6IZjDtP
Goの言語思想 “they are just regular numbers” 型なし定数の数値は「ただの数」である const HUGE =
92233720368547758070 const ROOT2 = 1.4142135623730950488016887242096980785696718753769 4807317667974 https://blog.golang.org/constants
Goの言語思想 “they live in a kind of ideal space of
values” 型なし定数は、型の制約のない自由な理想空間 で生きている
Goの言語思想 1 1.000 1e3-99.0*10-9 '\x01' ‘a’ (= ‘0x97 ’ 97)
'\u0001' 'b' - 'a' 1.0+3i-3.0i これらはどれも 型なし定数 1 と同等に扱える
おまけ この辺は例外的な仕様 • string(97) // => "a" • string(97.0) //
コンパイルエラー
ご清聴 ありがとうございました