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
500
Go言語低レイヤー入門 Hello world が 画面に表示されるまで / Introduction to low level programming in Go
dqneo
6
1.6k
入門Go言語仕様 Underlying Type / Go Language Underlying Type
dqneo
9
4.9k
How to write a self hosted Go compiler from scratch (Gophercon 2020)
dqneo
3
1.6k
もっと気軽に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
技術選定を未来に繋いで活用していく
sakito
3
100
Building a macOS screen saver with Kotlin (Android Makers 2025)
zsmb
1
110
フロントエンドテストの育て方
quramy
11
2.9k
Going Structural with Named Tuples
bishabosha
0
190
Java 24まとめ / Java 24 summary
kishida
3
390
CRE Meetup!ユーザー信頼性を支えるエンジニアリング実践例の発表資料です
tmnb
0
590
Building Scalable Mobile Projects: Fast Builds, High Reusability and Clear Ownership
cyrilmottier
2
190
タイムゾーンの奥地は思ったよりも闇深いかもしれない
suguruooki
1
390
海外のアプリで見かけたかっこいいTransitionを真似てみる
shogotakasaki
1
150
custom_lintで始めるチームルール管理
akaboshinit
0
200
新卒から4年間、20年もののWebサービスと 向き合って学んだソフトウェア考古学
oguri
8
7.1k
趣味全開のAITuber開発
kokushin
0
180
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
30
1.1k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
7
630
Designing for humans not robots
tammielis
251
25k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
118
51k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Site-Speed That Sticks
csswizardry
4
470
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.6k
Optimizing for Happiness
mojombo
377
70k
Java REST API Framework Comparison - PWX 2021
mraible
29
8.5k
Optimising Largest Contentful Paint
csswizardry
35
3.2k
The Invisible Side of Design
smashingmag
299
50k
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) //
コンパイルエラー
ご清聴 ありがとうございました