GO HISTORY
• What is the history of the project?
Robert Griesemer, Rob Pike and Ken Thompson started sketching the goals for a new language on the white
board on September 21, 2007. Within a few days the goals had settled into a plan to do something and a fair idea
of what it would be. Design continued part-time in parallel with unrelated work. By January 2008, Ken had
started work on a compiler with which to explore ideas; it generated C code as its output. By mid-year the
language had become a full-time project and had settled enough to attempt a production compiler. In May 2008,
Ian Taylor independently started on a GCC front end for Go using the draft specification. Russ Cox joined in late
2008 and helped move the language and libraries from prototype to reality.
Go became a public open source project on November 10, 2009. Countless people from the community have
contributed ideas, discussions, and code.
There are now millions of Go programmers—gophers—around the world, and there are more every day. Go's
success has far exceeded our expectations.
(By https://golang.org/doc/faq#history)
Robert Griesemer Rob Pike Ken Thompson
Slide 6
Slide 6 text
GO HISTORY
• What is the history of the project?
Robert Griesemer, Rob Pike and Ken Thompson started sketching the goals for a new language on the white
board on September 21, 2007. Within a few days the goals had settled into a plan to do something and a fair idea
of what it would be. Design continued part-time in parallel with unrelated work. By January 2008, Ken had started
work on a compiler with which to explore ideas; it generated C code as its output. By mid-year the language had
become a full-time project and had settled enough to attempt a production compiler. In May 2008, Ian Taylor
independently started on a GCC front end for Go using the draft specification. Russ Cox joined in late 2008 and
helped move the language and libraries from prototype to reality.
Go became a public open source project on November 10, 2009. Countless people from the community have
contributed ideas, discussions, and code.
There are now millions of Go programmers—gophers—around the world, and there are more every day. Go's
success has far exceeded our expectations.
(By https://golang.org/doc/faq#history)
ロバート・グリーズマーのツイート(Fumiの意訳)
今⽇(9⽉21⽇)から12年前、2007年9⽉20⽇、
私は私が信頼しているロブ・パイク、ケン・トンプソンとともに数分間仕事を忘れ、
そしてその時より良いコードが書ける⽅法を決めた。
9⽉25⽇にロブはGoという名前を提案し、それがすべての始まりだ。右肩上がり、前向きに進む、苦あれば楽あり。
Slide 7
Slide 7 text
よく⾔われること
Goが成功している理由
Goの成功している理由
• コンパイルの速さ
• 実⾏の速さ
• デプロイの容易さ
• ツール(go tool)
• 標準ライブラリ
(By Go Conference 2014 Autumn)
⾔語の特徴
• 並⾏プログラム
• インターフェース
etc.
Pob Pike⽒が考える成功の理由
GoのSimplicity(単純さ)
Slide 8
Slide 8 text
Goの世界を旅しよう︕
A TOUR OF GO
Slide 9
Slide 9 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
Slide 10
Slide 10 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
説明
実⾏環境
(エディタ)
Slide 11
Slide 11 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
最初書いてあったコードにリセットされる。
Slide 12
Slide 12 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
Go標準ツール gofmt でコードを整形する
import ONの時は、 goimports が使われる
Slide 13
Slide 13 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
コードを実⾏する
Slide 14
Slide 14 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
「Imports Off」と表⽰してある時︓
機能OFF状態
「Imports On」と表⽰してある時︓
「Format」をクリックして整形する際に、
必要なパッケージのimport⽂を⾃動で書いてくれ、
不要な場合は取り除いてくれる
Slide 15
Slide 15 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
https://tour.golang.org/
「Syntax Off」と表⽰してある時︓
機能OFF状態
「 Syntax On」と表⽰してある時︓
エディタ内のコードをシンタックスハイライトする
Slide 16
Slide 16 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
• 基本構⽂
– パッケージ、変数、関数
– 制御⽂︓for, if, else, switch, defer
– その他の型︓struct, slices, maps
• メソッドとインタフェース
– メソッドとインタフェース
• 並⾏性
– 並⾏性
A Tour of Goの構成
Slide 17
Slide 17 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
• 基本構⽂
– パッケージ、変数、関数
– 制御⽂︓for, if, else, switch, defer
– その他の型︓struct, slices, maps
• メソッドとインタフェース
– メソッドとインタフェース
• 並⾏性
– 並⾏性
本
⽇
の
範
囲
A Tour of Goの構成
※練習問題(Exercise)はやりません
Slide 18
Slide 18 text
休
憩
タ
イ
ム
Slide 19
Slide 19 text
A TOUR OF GO
⼀緒にGoの世界を冒険しよう︕
• 基本構⽂
– パッケージ、変数、関数
– 制御⽂︓for, if, else, switch, defer
– その他の型︓struct, slices, maps
• メソッドとインタフェース
– メソッドとインタフェース
• 並⾏性
– 並⾏性
本
⽇
の
範
囲
A Tour of Goの構成
※練習問題(Exercise)はやりません
TYPE
CONVERSIONS
math.Sqrt関数はfloat64を型に取るため、
intからfloat64へ型変換を⾏なっている
Goでの型変換は、
明⽰的な型変換を強制されている
型変換
変数 v 、型 T があった場合、
T(v) は、変数 v を T 型へ変換
暗黙的な型変換が⾏われないため、
適切な型を扱ってコードが書ける
ゆえに、バグを作りにくくしている
Slide 54
Slide 54 text
TYPE
INFERENCE
明⽰的な型を指定せずに変数を宣⾔する場合
( := や var = で初期化⼦を与えて宣⾔した場合)、
変数の型は右側の変数から型推論される
Slide 55
Slide 55 text
TYPE
INFERENCE
右側の変数が型を持っている場合 右側に型を指定しない定数である場合
var i int
j := i // j は i と同じint型になる
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128
Slide 56
Slide 56 text
TYPE
INFERENCE
fmt.Printf(“v is of type %T¥n”, v )
書式演算⼦のひとつ
「%T」は値の型をGoの構⽂で表現する
FOR
for i := 0 ; i < 10 ; i++ {
…
}
初期化⽂
(the init statement)
条件式
(the condition expression)
後処理⽂
(the post statement)
for ⽂は、セミコロン(;)によって、以下の3つの部分に分かれている
Slide 69
Slide 69 text
FOR
for i := 0 ; i < 10 ; i++ {
…
}
初期化⽂
(the init statement)
条件式
(the condition expression)
後処理⽂
(the post statement)
for ⽂は、セミコロン(;)によって、以下の3つの部分に分かれている
短い変数宣⾔によく利⽤する
その変数はfor⽂のスコープ内のみ有効
Slide 70
Slide 70 text
FOR
for i := 0 ; i < 10 ; i++ {
…
}
初期化⽂
(the init statement)
条件式
(the condition expression)
後処理⽂
(the post statement)
for ⽂は、セミコロン(;)によって、以下の3つの部分に分かれている
for ループの処理(下記では「…」のところ)が
終わった後に実⾏する処理
Slide 71
Slide 71 text
FOR
for i := 0 ; i < 10 ; i++ {
…
}
初期化⽂
(the init statement)
条件式
(the condition expression)
後処理⽂
(the post statement)
for ⽂は、セミコロン(;)によって、以下の3つの部分に分かれている
条件式が
falseになるまで
繰り返される
Slide 72
Slide 72 text
FOR
for i := 0 ; i < 10 ; i++ {
…
}
true
false
※上記は正しいフローチャートの
書き⽅ではありません
Slide 73
Slide 73 text
FOR
CONTINUED
• 初期化⽂と後処理⽂の記述は任意
Slide 74
Slide 74 text
FOR IS
GO’S “WHILE”
• 初期化⽂と後処理⽂の記述がない時セミコロン(;)も省略できる
つまり、C⾔語などにある while はGoでは for だけを使う
Slide 75
Slide 75 text
FOREVER
• ループ条件を省略すれば、無限ループ( infinite loop )になる。
Slide 76
Slide 76 text
IF
• if⽂はfor⽂同様、丸括弧は不要
Slide 77
Slide 77 text
IF WITH
A SHORT
STATEMENT
• for⽂のように条件式の前に、初期化⽂を書ける
• 初期化⽂で宣⾔した変数は、if⽂のスコープ内のみ有効
Slide 78
Slide 78 text
IF AND ELSE
• if⽂の初期化⽂で宣⾔した変数は
elseブロック内でも使⽤可能である
※ else if も使⽤可能
Slide 79
Slide 79 text
SWITCH
• switch⽂を使うことでIf-else⽂と同等のことを
シンプルにかける
• switch⽂もfor⽂のように条件式の前に、
初期化⽂を書ける
• 他⾔語とは違い、
選択された case だけを実⾏して
それに続く全ての case は実⾏されない
• caseも定数である必要はなく、
関係する値は整数である必要もない
SWITCH
EVALUATIONS
ORDER
※Go playground上の時間は、
いつも "2009-11-10 23:00:00 UTC"
(問題)
左記が⽰している⽇時は何の⽇でしょう︖
「Go 1」が
リリースされた⽇
Goが公のオープンソース
プロジェクトになった⽇
Goという名前がついた⽇
A
B
C
Slide 82
Slide 82 text
SWITCH
EVALUATIONS
ORDER
※Go playground上の時間は、
いつも "2009-11-10 23:00:00 UTC"
(問題)
左記が⽰している⽇時は何の⽇でしょう︖
「Go 1」が
リリースされた⽇
Goが公のオープンソース
プロジェクトになった⽇
Goという名前がついた⽇
A
B
C
2012.03.28
2007.09.25
CREATING
A SLICE
WITH MAKE
組み込み関数であるmake関数を使うことで、
動的サイズの配列を作成できる
Slide 154
Slide 154 text
(by https://golang.org/pkg/builtin/)
Slide 155
Slide 155 text
フィールド 値
array 0x00456000
len 5
cap 5
a
出⼒
a len=5 cap=5 [0 0 0 0 0]
メモリアドレス 値
0x00456000 0
0x00456004 0
0x00456008 0
0x0045600C 0
0x00456010 0
a orignal
Slide 156
Slide 156 text
フィールド 値
array 0x00456040
len 0
cap 5
b
出⼒
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
b orignal
メモリアドレス 値
0x00456040 0
0x00456044 0
0x00456048 0
0x0045604C 0
0x00456050 0
Slide 157
Slide 157 text
出⼒
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0]
フィールド 値
array 0x00456040
len 0
cap 5
b b orignal
メモリアドレス 値
0x00456040 0
0x00456044 0
0x00456048 0
0x0045604C 0
0x00456050 0
フィールド 値
array 0x00456040
len 2
cap 5
c
Slide 158
Slide 158 text
出⼒
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0]
d len=3 cap=3 [0 0 0]
フィールド 値
array 0x00456040
len 0
cap 5
b b orignal
メモリアドレス 値
0x00456040 0
0x00456044 0
0x00456048 0
0x0045604C 0
0x00456050 0
フィールド 値
array 0x00456040
len 2
cap 5
c
フィールド 値
array 0x00456048
len 3
cap 3
d
Slide 159
Slide 159 text
SLICES
OF SLICES
スライスには要素として
他のスライスを含む任意のタイプを
含めることができる
多次元スライスが表現可能
Slide 160
Slide 160 text
APPENDING
TO A SLICE
スライスへ新しい要素を追加する
Slide 161
Slide 161 text
(by https://golang.org/pkg/builtin/)
Slide 162
Slide 162 text
フィールド 値
array nil
len 0
cap 0
s
メモリアドレス 値
s orignal
出⼒
len=0 cap=0 []
Slide 163
Slide 163 text
フィールド 値
array 0x00414028
len 1
cap 2
s
メモリアドレス 値
0x00414028 0
0x0041402C 0
s orignal
出⼒
len=0 cap=0 []
len=1 cap=2 [0]
容量が不⾜しているため、
新たに配列が割り当てられた
Slide 164
Slide 164 text
フィールド 値
array 0x00414028
len 1
cap 2
s
メモリアドレス 値
0x00414028 0
0x0041402C 0
s orignal
出⼒
len=0 cap=0 []
len=1 cap=2 [0]
容量が不⾜しているため、
新たに配列が割り当てられた
新たに割り当てられる容量は、
おおよそ2倍ずつ確保される
Slide 165
Slide 165 text
フィールド 値
array 0x00414028
len 1
cap 2
s
メモリアドレス 値
0x00414028 0
0x0041402C 0
s orignal
出⼒
len=0 cap=0 []
len=1 cap=2 [0]
容量が不⾜しているため、
新たに配列が割り当てられた
新たに割り当てられる容量は、
おおよそ2倍ずつ確保される
「TCMalloc (Thread-Caching Malloc)」
というアルゴリズムを使って、高速に動的メモリを確保している
TCMallocについての参考:
Google が公開しているソフトウェアの解説(その4)-
Performance tools -
(https://japan.googleblog.com/2009/05/google-4-performance-
tools.html)
の後半でTCMallocについて説明されている
Slide 166
Slide 166 text
フィールド 値
array 0x00414028
len 2
cap 2
s
メモリアドレス 値
0x00414028 0
0x0041402C 1
s orignal
出⼒
len=0 cap=0 []
len=1 cap=2 [0]
len=2 cap=2 [0 1]
容量があるため、
スライスし直されて
値がセットされた
pos
neg
func(x int) int {
sum += x
return sum
}
func(x int) int {
sum += x
return sum
}
sum int
sum int
出⼒
0 0
1 -2
3 -6
6 -12
10 -20
15 -30
21 -42
28 -56
36 -72
45 -90
THANKS
• A Tour of Go
(http://tour.golang.org/)
• A Tour of Go (⽇本語翻訳版)
(https://go-tour-jp.appspot.com)
• gopherize.me
(https://gopherize.me/)
• egonelbre/gophers
(https://github.com/egonelbre/gophers)