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
楽しく向き合う例外対応
okutsu
0
630
未経験でSRE、はじめました! 組織を支える役割と軌跡
curekoshimizu
1
150
はじめての Go * WASM *OCR
sgash708
1
100
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
860
CI改善もDatadogとともに
taumu
0
200
Domain-Driven Transformation
hschwentner
2
1.9k
Jakarta EE meets AI
ivargrimstad
0
390
CloudNativePGを布教したい
nnaka2992
0
110
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
200
Jakarta EE meets AI
ivargrimstad
0
340
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
7
4.2k
DRFを少しずつ オニオンアーキテクチャに寄せていく DjangoCongress JP 2025
nealle
2
270
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
570
Java REST API Framework Comparison - PWX 2021
mraible
29
8.4k
What's in a price? How to price your products and services
michaelherold
244
12k
Mobile First: as difficult as doing things right
swwweet
223
9.4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Six Lessons from altMBA
skipperchong
27
3.6k
Side Projects
sachag
452
42k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
10
1.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
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) //
コンパイルエラー
ご清聴 ありがとうございました