Go (プログラミング言語 )
Goのロゴ | |
パラダイム |
|
---|---|
2009 | |
Robert Griesemer、ロブ・パイク、ケン・トンプソン | |
1.22.4[1] / 2024 | |
| |
gc、gccgo | |
C | |
Crystal | |
プラットフォーム | DragonFly BSD、FreeBSD、Linux、macOS、NetBSD、OpenBSD、Plan 9 from Bell Labs、Solaris、Microsoft Windows、iOS、Android、AIX、illumos |
ライセンス | BSDライセンス |
ウェブサイト | |
go |
Goはプログラミング
また、
名称 [編集 ]
しばしばGolangやGo
また、
歴史 [編集 ]
2009
- GoogleのGoツールチェイン。
複数 のプラットフォームをサポートとしており、Linux、BSD、macOS、Plan 9、Windows、そして(2015年 から)モバイルデバイスで動作 する[18]。このGoコンパイラは、バージョン1.5からセルフホスティングされるようになった[19]。 - 2つ
目 のコンパイラgccgo。これは、GCCのフロントエンドである[20]。
Goの
- JavaやC++のように、
静的 に型付 けされ、巨大 なシステムでもスケールする - RubyやPythonなどの
動的 な言語 のように生産 性 が高 く、リーダブルであり、過度 なボイラープレートが必要 ない[22] - IDEが
必須 ではない。ただし、十分 にサポートする - ネットワークおよびマルチプロセッシングをサポートする
バージョン1.6から、64ビットMIPS
2017
2018
2021
コード例 [編集 ]
Hello world[編集 ]
package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}
並行 性 の例 [編集 ]
package main
import (
"fmt"
"time"
)
func readword(ch chan string) {
fmt.Println("Type a word, then hit Enter.")
var word string
fmt.Scanf("%s", &word)
ch <- word
}
func timeout(t chan bool) {
time.Sleep(5 * time.Second)
t <- true
}
func main() {
t := make(chan bool)
go timeout(t)
ch := make(chan string)
go readword(ch)
select {
case word := <-ch:
fmt.Println("Received", word)
case <-t:
fmt.Println("Timeout.")
}
}
言語 機能 の特徴 [編集 ]
文法 [編集 ]
構造 体 [編集 ]
Goにはユーザー
package main
import "fmt"
// 新 しい構造 体型 の定義 。各 フィールドの識別子 とその型名 を指定 。
type Person struct {
name string // 名前 。
age int // 年齢 。
}
// メソッドの定義 。
func (m *Person) PrintName() {
fmt.Printf("Name = %s\n", m.name)
//fmt.Printf("Name (%T) = %#v\n", m.name, m.name)
}
func (m *Person) PrintAge() {
fmt.Printf("Age = %d\n", m.age)
//fmt.Printf("Age (%T) = %#v\n", m.age, m.age)
}
func main() {
var person Person
person.name = "John Smith"
person.age = 24
// 以下 のように書 くこともできる。
//person := Person{ "John Smith", 24 }
person.PrintName()
person.PrintAge()
// 以下 のように書式 指定 すると、型名 とリテラル表現 を出力 できる。
fmt.Printf("person (%T) = %#v\n", person, person)
}
name
およびage
というフィールドをPerson
PrintName()
およびPrintAge()
というメソッドを
Goでは、(m *Person)
のようなレシーバーの(
と
Goには
新 しい型 の定義 [編集 ]
Goではtype
int
MathInt
という
type MathInt int
これはC/C++におけるtypedefやC#におけるusingエイリアスディレクティブとint
int
package main
import (
"fmt"
"math"
)
type MathInt int
func (x MathInt) Abs() float64 {
return math.Abs(float64(x))
}
func main() {
var x MathInt = -10
fmt.Printf("%d\n", x) // -> -10
fmt.Printf("%f\n", x.Abs()) // -> 10.000000
fmt.Printf("%T\n", x) // -> main.MathInt
}
Go 1.9ではtypeキーワードを
package main
import (
"fmt"
"math"
)
type MyInt = int
func main() {
var x MyInt = -10
fmt.Printf("%d\n", x) // -> -10
fmt.Printf("%f\n", math.Abs(float64(x))) // -> 10.000000
fmt.Printf("%T\n", x) // -> int
}
匿名 フィールド[編集 ]
Goには
type Base int
func (_ *Base) Function() {
}
type Derived struct {
Base
}
// 呼 び出 し例
var derived Derived
derived.Function()
このようにして
interface[編集 ]
GoにはC++や
interface
Goにおけるinterfaceの
type Container interface {
Begin() Iterator
End() Iterator
}
Goのinterfaceは、MLのsignatureや、かつてg++に
defer[編集 ]
Goではdefer
defer
defer
キーワードに
ファイルのオープンとクローズなど、
defer
は
defer 関数 呼 び出 しまたはメソッド呼 び出 し
defer
はブロックをdefer
をdefer
で
defer
には、
defer func() {
message := "done"
fmt.Println(message)
}()
例外 処理 [編集 ]
try-catch
のような
ダウンキャスト[編集 ]
Goはインターフェース
// Value の返 値 はどんな型 でも構 わない
var i interface{} = Value()
/*
i の基底 型 を string と仮定 し、
実際 にそうであれば s には変換 結果 が、 ok には true がセットされ、
そうでなければ s にはstring型 の初期 値 (ゼロ値 )が、 ok には false がセットされる。
以下 の様 に2つ目 の返 値 を無視 すると、i の基底 型 が string でなければ暗黙 的 にランタイムパニックとなる
s := i.(string)
*/
if s, ok := i.(string); ok {
fmt.Println ("文字 列 : ", s)
} else {
var kind string
// 型 switch
switch i.(type) {
case int:
kind = "数値 "
default:
kind = "その他 "
}
fmt.Printf ("%s: %v\n", kind, i)
}
名前 とモジュールの管理 [編集 ]
Goには
package main // 現在 のソースファイルが所属 するパッケージ名 の指定
// 本 記事 の例 では省略 されている事 が多 いが本来 は必須 である
import _ "./sub/a" // (1)ソースファイル中 から名前 が使用 されないパッケージの取 り込 み
import . "./sub/b" // (2)パッケージ名 を省略 して、パッケージ内 の名前 を現在 のソースファイルに全 て取 り込 む
import alias_c "./sub/c" // (3)パッケージ名 に別名 を付 けてパッケージ名 を現在 のソースファイルに取 り込 む
import "./sub/d" // (4)パッケージ名 に末端 のパッケージ名 と同 じ名前 を付 けて現在 のソースファイルに取 り込 む
func main() {
Function() // (2)のパッケージに所属 するFunctionの呼 び出 し
alias_c.Function() // (3)のパッケージに所属 するFunctionの呼 び出 し
d.Function() // (4)のパッケージに所属 するFunctionの呼 び出 し
}
Goにはimport
Goの
init
)をmain
import
されたパッケージに
アクセス指定 子 [編集 ]
Go には、
提供 しない機能 [編集 ]
処理 系 [編集 ]
Go
gc
はGo
Gccgo
は、
脚注 [編集 ]
注釈 [編集 ]
出典 [編集 ]
- ^ “Release History”. Template:Cite webの
呼 び出 しエラー:引数 accessdate は必須 です。 - ^ “Why doesn't Go have "implements" declarations?”. go.dev. 2015
年 10月 1日 閲覧 。 - ^ Pike, Rob (2014
年 12月22日 ). “Rob Pike on Twitter”. 2016年 3月 13日 閲覧 。 “Go has structural typing, not duck typing. Full interface satisfaction is checked and required.” - ^ Kincaid, Jason (2009
年 11月10日 ). “Google’s Go: A New Programming Language That’s Python Meets C++”. TechCrunch 2010年 1月 18日 閲覧 。 - ^ “Language Design FAQ”. go.dev (2010
年 1月 16日 ). 2010年 2月 27日 閲覧 。 - ^ Metz, Cade (2011
年 5月 5日 ). “Google Go boldly goes where no code has gone before”. The Register. 2018年 7月 24日 閲覧 。 - ^ go.dev
- ^ go installation guide
- ^ “Go version 1 is released” (2012
年 3月 28日 ). 2012年 3月 29日 閲覧 。 - ^ “プログラミング
言語 「Go 1.4」がリリース、Androidを正式 にサポート” (2014年 12月12日 ). 2015年 8月 26日 閲覧 。 - ^ “Go 1.5 is released” (2015
年 8月 19日 ). 2015年 8月 26日 閲覧 。 - ^ Google App Engine Blog, Tuesday, May 10, 2011
- ^ “Go 1.11 is released”. The Go Blog (2018
年 8月 24日 ). 2018年 10月 4日 閲覧 。 - ^ a b c Frequently Asked Questions (FAQ) - The Go Programming Language
- ^ “Hey! Ho! Let's Go!”. Google Open Source. Google. 2018
年 5月 17日 閲覧 。 - ^ Gerrand. “Go version 1 is released - The Go Blog”. 2018
年 2月 4日 閲覧 。 Shankland, Stephen (2012年 3月 30日 ). “Google's Go language turns one, wins a spot at YouTube: The lower-level programming language has matured enough to sport the 1.0 version number. And it's being used for real work at Google.”. CBS Interactive Inc (2012-03-30発行 ) 2017年 8月 6日 閲覧 . "Google has released version 1 of its Go programming language, an ambitious attempt to improve upon giants of the lower-level programming world such as C and C++." “Release History”. 2018年 7月 24日 閲覧 。 - ^ “Go FAQ: Is Google using Go internally?” 2013
年 3月 9日 閲覧 。 - ^ “Google's In-House Programming Language Now Runs on Phones”. wired.com (2015
年 8月 19日 ). 2018年 7月 24日 閲覧 。 - ^ “Go 1.5 Release Notes”. 2016
年 1月 28日 閲覧 。 “The compiler and runtime are now implemented in Go and assembler, without C.” “Go 1.5が登場 、Androidに続 いてiOSアプリも開発 可能 。ガベージコレクションも大幅 に改善 ” (2015年 8月 24日 ). 2015年 8月 26日 閲覧 。 - ^ “FAQ: Implementation”. go.dev (2010
年 1月 16日 ). 2010年 1月 18日 閲覧 。 “Installing GCC: Configuration”. 2011年 12月3日 閲覧 。 “Ada, Go and Objective-C++ are not default languages” - ^ Pike, Rob (2010
年 4月 28日 ). “Another Go at Language Design”. Stanford EE Computer Systems Colloquium. Stanford University. 2018年 7月 24日 閲覧 。 Video available. - ^ “Frequently Asked Questions (FAQ) - The Go Programming Language”. go.dev. 2016
年 2月 26日 閲覧 。 - ^ Andrew Binstock (2011
年 5月 18日 ). “Dr. Dobb's: Interview with Ken Thompson”. 2014年 2月 7日 閲覧 。 Pike, Rob (2012年 ). “Less is exponentially more”. 2018年 7月 24日 閲覧 。 Robert Griesemer (2015年 ). “The Evolution of Go”. 2018年 7月 24日 閲覧 。 - ^ Go 1.6 Release Notes - The Go Programming Language
- ^
末岡 洋子 (2016年 2月 19日 ). “「Go 1.6」リリース、細 かな性能 改善 などがメインのリリースに”. 2016年 2月 20日 閲覧 。 - ^ Cox, Russ. “Toward Go 2 - The Go Blog”. 2018
年 2月 4日 閲覧 。 - ^ “Go 1.17 is released - go.dev”. go.dev. 2021
年 9月 19日 閲覧 。 - ^ Chisnall, David (2012). The Go Programming Language Phrasebook. Addison-Wesley
- ^ a b https://go.dev/doc/faq
- ^ Go 1.9 Release Notes - The Go Programming Language
- ^ reflect - The Go Programming Language
- ^ §Implementation | Frequently Asked Questions (FAQ) - The Go Programming Language
関連 項目 [編集 ]
解説 書籍 など[編集 ]
松木 雅幸 、mattn、藤原 俊一郎 、中島 大 一 、上田 拓也 、牧 大輔 、鈴木 健太 :「改訂 2版 みんなのGo言語 」、技術評論社 、ISBN 978-4-297-10727-7(2019年 8月 1日 )。- Alan A.A.Donovan, Brian W.Kernighan
著 、柴田 芳樹 翻訳 :「プログラミング言語 Go」、丸善 出版 、ISBN 978-4-621-30025-1(2016年 6月 20日 )。
Goのtourをgo install golang.org/x/website/tour@latest
を