Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Go Conference 2024] golang/goのbuiltin packageを...

[Go Conference 2024] golang/goのbuiltin packageを覗いてみる (20240608_Go_Conference_2024_builtin_package)

2024年6月8日 (土) に開催されたGo Conference 2024の発表資料です。

https://gocon.jp/2024/

プロポーザル

github.com/golang/goにはコンパイラやgofmtをはじめとする各種ツール、標準ライブラリなどが含まれています。その中から、builtin package (src/builtin/) を紹介します。Goを実装している際、エディタやIDEの機能で組み込み型の定義を開いたことがある方は少なくないと思います。そのときにたどり着くのがこのpackageです。

builtin packageは、predeclared identifiers (直訳すると事前に宣言された識別子) が定義されたpackageです。bool, uint8, float64, stringといった組み込み型や、append, lenといった組み込み関数が定義されていますが、その実装はこのpackageには含まれていません。本LTでは、これらの定義がpackage内でどのように書かれているか紹介し、このpackageが存在する理由を簡単に説明します。

Koki Senda

June 08, 2024
Tweet

More Decks by Koki Senda

Other Decks in Programming

Transcript

  1. 話は変わって 
 • エディタ等で「定義に移動」
 個人的なGoで開発中あるある 
 func main() { person

    := Person{ Name: "Ichiro", } fmt.Println("Hello,", person.Name) } このPersonってなんだろう? 

  2. 話は変わって 
 • エディタ等で「定義に移動」
 個人的なGoで開発中あるある 
 func main() { person

    := Person{ Name: "Ichiro", } fmt.Println("Hello,", person.Name) } type Person struct { Name string }
  3. 話は変わって 
 • エディタ等で「定義に移動」
 個人的なGoで開発中あるある 
 func main() { person

    := Person{ Name: "Ichiro", } fmt.Println("Hello,", person.Name) } type Person struct { Name string } このstringってなんだろう? 

  4. 話は変わって 
 • エディタ等で「定義に移動」 → 繰り返すうちに基本型までたどり着いた 
 個人的なGoで開発中あるある 
 func

    main() { person := Person{ Name: "Ichiro", } fmt.Println("Hello,", person.Name) } type Person struct { Name string } // string is the set of all strings of 8-bit bytes, conventionally but not // necessarily representing UTF-8-encoded text. A string may be empty, but // not nil. Values of string type are immutable. type string string
  5. 話は変わって 
 • エディタ等で「定義に移動」 → 繰り返すうちに基本型までたどり着いた 
 個人的なGoで開発中あるある 
 func

    main() { person := Person{ Name: "Ichiro", } fmt.Println("Hello,", person.Name) } type Person struct { Name string } // string is the set of all strings of 8-bit bytes, conventionally but not // necessarily representing UTF-8-encoded text. A string may be empty, but // not nil. Values of string type are immutable. type string string ここが `builtin ` パッケージ

  6. 例) builtinパッケージに書いてあるもの 
 • 基本型
 ◦ bool
 ◦ uint8
 ◦

    float64
 ◦ string
 • 組み込み関数
 ◦ make
 ◦ append
 • 定数
 ◦ true
 ◦ false
 ◦ iota
 基本型、組み込み関数、定数など 

  7. builtinパッケージにどう書いてあるか? 
 例えば uint8 ならこう
 基本型、組み込み関数、定数など 
 // uint8 is

    the set of all unsigned 8-bit integers. // Range: 0 through 255. type uint8 uint8
 uint8をuint8と定義する??

  8. builtinパッケージにどう書いてあるか? 
 例えば len ならこう
 基本型、組み込み関数、定数など 
 // The len

    built-in function returns the length of v, according to its type: // // Array: the number of elements in v. // Pointer to array: the number of elements in *v (even if v is nil). // Slice, or map: the number of elements in v; if v is nil, len(v) is zero. // String: the number of bytes in v. // Channel: the number of elements queued (unread) in the channel buffer; // if v is nil, len(v) is zero. // // For some arguments, such as a string literal or a simple array expression, the // result can be a constant. See the Go language specification's "Length and // capacity" section for details. func len(v Type) int

  9. builtinパッケージにどう書いてあるか? 
 例えば len ならこう
 基本型、組み込み関数、定数など 
 // The len

    built-in function returns the length of v, according to its type: // // Array: the number of elements in v. // Pointer to array: the number of elements in *v (even if v is nil). // Slice, or map: the number of elements in v; if v is nil, len(v) is zero. // String: the number of bytes in v. // Channel: the number of elements queued (unread) in the channel buffer; // if v is nil, len(v) is zero. // // For some arguments, such as a string literal or a simple array expression, the // result can be a constant. See the Go language specification's "Length and // capacity" section for details. func len(v Type) int
 実装は書かれていない

  10. builtinパッケージとは 
 • Predeclared Identifiers
 ◦ 直訳すると事前に宣言された識別子
 • 場所は src/builtin/builtin.go


    • 実装はここには書いていない!
 ◦ Goの基本動作に関わるので、これらの振る舞いはコンパイラの中に 
 • ではなぜ書いてあるのか?
 ◦ 主にドキュメンテーションのため 
 ◦ ここに定義されているとgodocでドキュメント化できる 
 Predeclared Identifiersのドキュメントを提供 

  11. true/falseの定数定義 
 おもしろかったので紹介 
 // true and false are the

    two untyped boolean values. const ( true = 0 == 0 // Untyped bool. false = 0 != 0 // Untyped bool. )