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
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
2
3.2k
PHPバージョンアップから始めるOSSコントリビュート / how2oss-contribute
dmnlk
1
890
Do Dumb Things
mitsuhiko
0
410
Day0 初心者向けワークショップ実践!ソフトウェアテストの第一歩
satohiroyuki
0
820
Coding Experience Cpp vs Csharp - meetup app osaka@9
harukasao
0
720
アプリを起動せずにアプリを開発して品質と生産性を上げる
ishkawa
0
2.5k
データベースエンジニアの仕事を楽にする。PgAssistantの紹介
nnaka2992
9
4.5k
S3静的ホスティング+Next.js静的エクスポート で格安webアプリ構築
iharuoru
0
220
アーキテクトと美学 / Architecture and Aesthetics
nrslib
12
3.3k
小さく段階的リリースすることで深夜メンテを回避する
mkmk884
2
160
英語 × の私が、生成AIの力を借りて、OSSに初コントリビュートした話
personabb
0
180
MCP世界への招待: AIエンジニアが創る次世代エージェント連携の世界
gunta
4
860
Featured
See All Featured
The Invisible Side of Design
smashingmag
299
50k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
135
33k
Gamification - CAS2011
davidbonilla
81
5.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Designing Experiences People Love
moore
141
23k
Code Reviewing Like a Champion
maltzj
522
39k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
12
650
Typedesign – Prime Four
hannesfritz
41
2.6k
The Cost Of JavaScript in 2023
addyosmani
48
7.7k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
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) //
コンパイルエラー
ご清聴 ありがとうございました