- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- Go へ行く。
- 1 (2018-12-05 (水) 08:28:26)
- 2 (2018-12-05 (水) 08:29:26)
- 3 (2018-12-10 (月) 00:57:44)
- 4 (2018-12-10 (月) 02:05:02)
- 5 (2018-12-31 (月) 22:39:01)
- 6 (2020-01-31 (金) 21:32:23)
- 7 (2020-02-24 (月) 12:30:25)
- 8 (2020-04-29 (水) 20:10:08)
- 9 (2020-04-30 (木) 01:53:54)
- 10 (2020-05-03 (日) 00:36:29)
- 11 (2020-05-03 (日) 07:02:59)
- 12 (2020-05-03 (日) 20:47:18)
Goの特徴 †
- Googleによって開発され2009年に発表されたプログラミング言語
- ネイティブコードにコンパイルされる
- マルチプラットフォームで動作する
- OSに依存しない、各OSの標準ライブラリに依存しない実行ファイルを生成する
- ガベージコレクションあり
- goroutineという並行処理の仕組みがあり、シンプルかつ効率的な処理が可能
- オブジェクト指向言語ではない
Go言語の基本 †
他の言語との差分で自分が気になった箇所のみ記述する。
言語仕様は、以下のURLに詳細にまとめられているのでそちらを参照のこと。
URL https://golang.org/ref/spec
エントリーポイント †
mainパッケージのmain関数がエントリーポイント
package main import "fmt" func main() { fmt.Printf("Hello, World\n") }
構文・関数 †
go文 †
並行処理のための構文。
スレッドよりも小さな単位であるgoroutineが並行して動作する。
package main import ( "fmt" "time" ) func main() { termCh := make(chan struct{}) finCh := make(chan struct{}) go func(termCh, finCh chan struct{}) { defer func() { fmt.Println("go: close(finCh)") close(finCh) }() i := 0 for { fmt.Printf("go: running go routine = %d\n", i) i = i + 1 time.Sleep(1 * time.Second) select { case <-termCh: fmt.Println("go: received stop signal from main") return default: } } }(termCh, finCh) times := 0 for { time.Sleep(800 * time.Millisecond) if times > 3 { fmt.Println("main: send stop signal") close(termCh) break } times = times + 1 } <-finCh fmt.Println("main: done") }
defer文 †
関数の終了時にdeferで指定された式を実行することができる。
リソースの解放処理などに使用できる。
package main import "fmt" func finalize() { fmt.Println("finalize") } func runDefer() { defer finalize() defer func() { fmt.Println("runDefer defer") }() fmt.Println("end runDefer()") } func main() { defer func() { fmt.Println("main defer") }() runDefer() fmt.Println("end main()") } // end runDefer() // runDefer defer // finalize // end main() // main defer
参考 https://golang.org/ref/spec#Defer_statements
iotaキーワード †
- ConstSpecのインデックス。
- const宣言ごとに0の値から始まる。
- constのブロック指定でない場合は、0になる。
package main import "fmt" const Out_block1 = iota // 0 (ConstSpec) const Out_block2 = iota // 0 (ConstSpec) const ( Blk1_a = iota // 0 (iota=0) (ConstSpec) Blk1_b = 0 // (iota=1) (ConstSpec) Blk1_c = iota // 2 (iota=2) (ConstSpec) ) const ( Blk2_a = iota // 0 Blk2_b = 0 // 0 Blk2_c = iota // 2 ) const ( Blk3_a1, Blk3_a2 = iota, iota // 0, 0 (ConstSpec) Blk3_b1, Blk3_b2 = iota, iota // 1, 1 (ConstSpec) ) func main() { fmt.Printf("Out_block1 = %d\n", Out_block1) fmt.Printf("Out_block2 = %d\n", Out_block2) fmt.Printf("Blk1_a = %d, Blk1_b = %d, Blk1_c = %d\n", Blk1_a, Blk1_b, Blk1_c) fmt.Printf("Blk2_a = %d, Blk2_b = %d, Blk2_c = %d\n", Blk2_a, Blk2_b, Blk2_c) fmt.Printf("Blk3_a1 = %d, Blk_a2 = %d\n", Blk3_a1, Blk3_a2) }
参考 https://golang.org/ref/spec#ConstSpec
init関数 †
init関数は、main関数の実行に先立って実行される。パッケージの初期化を目的にした特殊な関数である。通常ならば関数の重複はエラーとなるが、init関数は複数定義することができるようだ。
package main import "fmt" func init() { fmt.Println("init") } func init() { fmt.Println("init2") } func main() { fmt.Printf("Hello world\n") } // init // init2 // Hello world
参考リンク †
- https://golang.org
- https://github.com/golang/go
- https://golang.org/doc/articles/wiki/
- https://go言語.com/doc/- on https://go言語.com/doc/
実装例など †
goroutineのパターンが分かりやすくまとめられている。
数多くの実装例が掲載されている。
初学者に分かりやすく役立つ情報 †
- A Tour of Go - on https://go-tour-jp.appspot.com/list
- Welcome to a tour of Go -on https://go-tour-jp.appspot.com/list
- Goならわかるシステムプログラミング - on http://ascii.jp/elem/000/001/235/1235262/
- Go入門 - on https://www.slideshare.net/takuyaueda967/2016-go
- Go言語の初心者が見ると幸せになれる場所 #golang - on https://qiita.com/tenntenn/items/0e33a4959250d1a55045