SlideShare a Scribd company logo
1 of 43
Download to read offline
Revised v4Presenter
プログラミング言語Go
鵜飼文敏 / Google Software Engineer
今日の内容
godentaku - Goで作った電卓
入出力の基本
sliceの使い方
型システム: struct, pointer, interface
goパッケージ
ローカル・サードパーティ
使い方・作り方
Go言語とは?
新しいシステムプログラミング言語
Googleが設計・開発
Rob Pike, Ken Thompson, Robert Griesemer,
Russ Cox, Ian Lance Taylerなど
2009/11/10にオープンソースとして公開
http://golang.org
コントリビュータ: 125名 (2010/09)
Go言語の特徴
静的型付け・型安全
ダックタイピング
短いコンパイル時間
大規模プログラミング可能
ネットワーキング
マルチコア
主流プログラミング言語
C++, Java
ライトウェイト言語
Python, Rubyなど
What's new?
DevFest (2010/3)から
complex型
x := m[k] がpanicしなくなった。
s[0:n] が s[:n]に
panic(), recover()
goinstallコマンド
gomakeコマンド ($GOOS, $GOARCH, ...)
goprofコマンド
Go dashboard
new package
crypto/ripemd160, hash/crc64, crypto/rand, utf16, nntp, mime/multipart, html, net/textp
roto, net/dict, crypto/ocsp, runtime/pprof, http/pprof, cmath, rpc/jsonrpc,
go/typechecker
once -> sync.Once()
The Go Playground
今日のお題:電卓
$ ./godentaku
>1+2*3
7
>y=x*x + (a+b)*x + a*b
(((x * x) + ((a + b) * x)) + (a * b))
>a=1
1
>b=2
2
>y
(((x * x) + (3 * x)) + 2)
>x=5
5
>y
42
godentaku
仕様
式を入力して、それを評価して出力
四則演算
変数
言語の基本機能で実装
パッケージをあまり使わない
(fmt, os, bufioのみ)
godentaku
入力
標準入力から一行づつバイト列を読み取る
式を解析
バイト列を抽象構文木に変換
式を評価
抽象構文木を解釈・実行
結果を出力
結果を文字列にして標準出力に
繰り返す
入力: bufio
http://golang.org/pkg/bufio/#Reader.ReadBytes
func (*Reader) ReadBytes
func (b *Reader) ReadBytes(delim byte) (line []byte, err os.Error)
ReadBytes reads until the first occurrence of delim in the input, returning a string containing the data up to and
including the delimiter. If ReadBytes encounters an error before finding a delimiter, it returns the data read
before the error and the error itself (often os.EOF). ReadBytes returns err != nil if and only if line does not end in
delim.
import ("bufio"; "os")
in := bufio.NewReader(os.Stdin)
line, err := in.ReadBytes('n')
if err != nil { ... }
出力: fmt
http://golang.org/pkg/fmt/#Printf
func Printf
func Printf(format string, a ...interface{}) (n int, errno os.Error)
Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and
any write error encountered.
import "fmt"
fmt.Printf("> ")
fmt.Printf("%d", i)
fmt.Printf("%#v", v)
コンパイル&実行
% cat main.go
package main
import ("bufio"; "fmt"; "os")
func main() {
in := bufio.NewReader(os.Stdin)
for {
line, err := in.ReadBytes('n')
if err != nil { break }
fmt.Println(string(line))
}
}
% 6g main.go # 8g main.go
% 6l main.6 # 8l main.8
% ./6.out # ./8.out
Read-eval-print loop
package main
import ("bufio"; "fmt"; "os")
func readEvalPrint(in *bufio.Reader, env *Env) os.Error
func main() {
in := bufio.NewReader(os.Stdin)
env := NewEnv() // for variable table
for {
err := readEvalPrint(in, env)
if err != nil { break }
}
}
Read-eval-print loop
func readEvalPrint(in *bufio.Reader, env *Env) os.Error {
fmt.Printf("> ")
line, err := in.ReadBytes('n')
if err != nil { return err }
ast, nbuf := Read(line)
v := Eval(ast, env)
fmt.Println(Print(v, env))
return nil
}
Read: getNum
func getNum(buf []byte) (n int, nbuf []byte) {
n := digitVal(buf[0])
nbuf = buf[1:]
for len(nbuf) > 0 {
if d := digitVal(nbuf[0]); d >= 0 {
n = n * 10 + d
} else { break }
nbuf = nbuf[1:]
}
return n, nbuf
}
func digitVal(b byte) int {
if b >= '0' && b <= '9' {
return int(b - '0') // int型に変換
} else { return -1 }
}
slice
sliceは配列につかうポインタ
Read: getSymbol
func getSymbol(buf []byte) (sym string, nbuf []byte) {
var i int
for i = 0; i < len(buf); i++ {
if !isAlpha(buf[i]) && !isDigit(buf[i]) {
break
}
}
return string(buf[0:i]), buf[i:]
}
抽象構文木
再帰降下パーザ
// stmt := expr | sym '=' expr
func parseStatement(b []byte) (stmt Ast, n []byte)
// expr := ['+'|'-'] term { ['+'|'-'] term }
func parseExpression(b []byte) (expr Ast, n []byte)
// term := factor { ['*'|'/'] factor }
func parseTerm(b []byte) (term Ast, n []byte)
// factor := num | symbol | '(' expr ')'
func parseFactor(b []byte) (factor Ast, n []byte)
interface: Ast
type Ast interface {
String() string
Eval(env* Env) Ast
}
Ast型の変数に代入できるのは、これらのメソッドが定義されて
いる型のオブジェクト
Astにいれて使う型はこれらのメソッドを定義
Astを使うほうは、これらのメソッドが利用可能
named type: Num
type Num int
func (n Num) String() string {
return fmt.Sprintf("%d", int(n))
}
func (n Num) Eval(env *Env) Ast { return n }
func getNum(b []byte) (num Num, nbuf []byte) {
...
return Num(n), nbuf
}
NumはAst型
named type: Symbol
type Symbol string
func (s Symbol) String() string {
return string(s)
}
func (s Symbol) Eval(env *Env) Ast { ... }
SymbolもAst型
map: variable table
type Env struct { Var map[string]Ast; .. }
func NewEnv() *Env {
env := new(Env)
env.Var = make(map[string]Ast)
return env
}
func Set(env* Env, key string, n int) {
env.Var[key] = Num(n)
}
func envValue(env* Env, key string) (n int, ok bool) {
if v, found := env.Var[key]; found {
..
}
return 0, false
}
structとポインタ
type Point struct { x, y int }
type Rect struct { min, max Point }
type Rect2 struct { min, max *Point }
BinOp
type Ast interface { .. }
type BinOp struct { Op byte; Left, Right Ast }
func (b BinOp) String() string {..}
func (b BinOp) Eval(env *Env) Ast { .. }
func parseExpression(b []byte) (expr Ast, n []byte) {
expr, n = parseTerm(b)
for n[0] == '+' || n[0] == '-' {
op := n[0]; var term Ast
term, n = parseTerm(n[1:])
expr = BinOp{Op: op, Left:expr, Right:term}
}
return expr, n
}
BinOpもAst型
interface: memory layout
interface type-switch
func parseStatement(b []byte) (stmt Ast, n []byte) {
stmt, n = parseExpression(b)
if n[0] == '=' {
if sym, ok := stmt.(Symbol); ok {
var expr Ast
expr, n = parseExpression(n[1:])
stmt = AssignOp{Var:sym, Expr:expr}
} else { /* 例外処理 */ }
}
return stmt, n
}
例外処理
func readEvalPrint(in *bufio.Reader, env *Env) os.Error {
defer func() { if x := recover(); x != nil { ... } }()
.. Read(line) ..
}
func Read(b []byte) (ast Ast, n[]byte) {
return parseStatement(b)
}
func parseStatement(b []byte) (stmt Ast, n []byte) {
...
if sym, ok := stmt.(Symbol); ok { ..
} else {
panic("lvalue is not symbol:" + stmt.String());
}
}
いままでのまとめ
入出力
bufio
fmt
型システム
int, string, ..
slice
map
struct
ポインタ
interface
packageにする
1. ファイルを分割する
メインのファイル (main.go)
パッケージのファイル (godentaku.go)
2. メインのファイル
import に "./godentaku"
godentaku.Read(line)
3. パッケージのファイル
package godentaku
大文字ではじまるシンボルがパブリック
packageのビルド
% 6g godentaku.go
% 6g main.go # main.goとgodentaku.6を読む
% ls
godentaku.6 godentaku.go
main.6 main.go
% 6l main.6
% ./6.out
packageディレクトリ
% mkdir godentaku; mv godentaku.* godentaku/; cd godentaku
% cat > Makefile
include $(GOROOT)/src/Make.inc
TARG=godentaku # importでつかうパッケージパス
GOFILES=
godentaku.go
include $(GOROOT)/src/Make.pkg
% gomake # コンパイルとリンク
% gomake install # -> ${GOROOT}/pkg/${GOOS}_${GOARCH}/${TARG}.a
packageの公開
標準packageにコントリビュート
codereview
http://golang.org/doc/contribute.html
サードパーティpackageとして公開
http://godashboard.appspot.com/package
サードパーティpackageの利用
インストール
% goinstall goprotobuf.googlecode.com/hg/proto
利用する時
import "goprotobuf.googlecode.com/hg/proto"
data, err := proto.Marshal(obj)
googlecode - project hosting
googlecode - createProject
Mercurial
https://yourproj.googlecode.com/hg
hgrc
% cat .hg/hgrc
[paths]
default = https://yourproj.googlecode.com/hg/
[ui]
username = Your Name <foo@example.com>
[auth]
googlecode.prefix = yourproj.googlecode.com/hg/
gogolecode.username = foo@example.com
googlecode.password = XXXXX
googlecode.schemes = https
publish package
1. プロジェクトの作成
2. hg clone
3. hgrcの編集
4. ファイルの追加
hg add
Makefileの編集
TARGを import path
5. hg commit
6. hg push
本日のまとめ
Go言語を使って
入出力の基本
型システムの基本
packageの使い方・作り方
googlecode project hostingで公開
今日やらなかったこと
並行処理: goroutineとchannel
Iota: enum的なもの
goツール
godoc, gofmt, gotest, goyacc など
cgo: Cコードを呼ぶgoパッケージの作成
標準パッケージの使い方
などなど
May the Source be with You

More Related Content

What's hot

組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由
kikairoya
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
fuzzysphere
 
エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7
Tetsuya Morimoto
 

What's hot (20)

LLdeade Python Language Update
LLdeade Python Language UpdateLLdeade Python Language Update
LLdeade Python Language Update
 
F#入門 ~関数プログラミングとは何か~
F#入門 ~関数プログラミングとは何か~F#入門 ~関数プログラミングとは何か~
F#入門 ~関数プログラミングとは何か~
 
Lisp Tutorial for Pythonista : Day 3
Lisp Tutorial for Pythonista : Day 3Lisp Tutorial for Pythonista : Day 3
Lisp Tutorial for Pythonista : Day 3
 
中3女子が狂える本当に気持ちのいい constexpr
中3女子が狂える本当に気持ちのいい constexpr中3女子が狂える本当に気持ちのいい constexpr
中3女子が狂える本当に気持ちのいい constexpr
 
String representation in py3k
String representation in py3kString representation in py3k
String representation in py3k
 
Cython intro prelerease
Cython intro prelereaseCython intro prelerease
Cython intro prelerease
 
組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由組み込みでこそC++を使う10の理由
組み込みでこそC++を使う10の理由
 
constexpr idioms
constexpr idiomsconstexpr idioms
constexpr idioms
 
BoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうかBoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうか
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
 
Cython ことはじめ
Cython ことはじめCython ことはじめ
Cython ことはじめ
 
C++14 Overview
C++14 OverviewC++14 Overview
C++14 Overview
 
20170131 python3 6 PEP526
20170131 python3 6 PEP526 20170131 python3 6 PEP526
20170131 python3 6 PEP526
 
One - Common Lispでもワンライナーしたい
One - Common LispでもワンライナーしたいOne - Common Lispでもワンライナーしたい
One - Common Lispでもワンライナーしたい
 
NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門NumPyが物足りない人へのCython入門
NumPyが物足りない人へのCython入門
 
boost::shared_ptr tutorial
boost::shared_ptr tutorialboost::shared_ptr tutorial
boost::shared_ptr tutorial
 
エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7
 
ひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指すひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指す
 
STLの型の使い分け(ダイジェスト版) @ Sapporo.cpp 第7回勉強会 (2014.10.18)
STLの型の使い分け(ダイジェスト版) @ Sapporo.cpp 第7回勉強会 (2014.10.18)STLの型の使い分け(ダイジェスト版) @ Sapporo.cpp 第7回勉強会 (2014.10.18)
STLの型の使い分け(ダイジェスト版) @ Sapporo.cpp 第7回勉強会 (2014.10.18)
 
Boost.勉強会 #21 札幌「C++1zにstring_viewが導入されてうれしいので紹介します」
Boost.勉強会 #21 札幌「C++1zにstring_viewが導入されてうれしいので紹介します」Boost.勉強会 #21 札幌「C++1zにstring_viewが導入されてうれしいので紹介します」
Boost.勉強会 #21 札幌「C++1zにstring_viewが導入されてうれしいので紹介します」
 

Viewers also liked

Viewers also liked (14)

Google Developer Day 2010 Japan: Google App Engine についての最新情報 (松尾貴史)
Google Developer Day 2010 Japan: Google App Engine についての最新情報 (松尾貴史)Google Developer Day 2010 Japan: Google App Engine についての最新情報 (松尾貴史)
Google Developer Day 2010 Japan: Google App Engine についての最新情報 (松尾貴史)
 
Fluency phrases 1a
Fluency phrases 1aFluency phrases 1a
Fluency phrases 1a
 
Google Developer Day 2010 Japan: クールな Android アプリを作るには (安生真, 山下盛史, 江川崇)
Google Developer Day 2010 Japan: クールな Android アプリを作るには (安生真, 山下盛史, 江川崇)Google Developer Day 2010 Japan: クールな Android アプリを作るには (安生真, 山下盛史, 江川崇)
Google Developer Day 2010 Japan: クールな Android アプリを作るには (安生真, 山下盛史, 江川崇)
 
Primeros auxilios
Primeros auxiliosPrimeros auxilios
Primeros auxilios
 
Pitney Bowes User Forums
Pitney Bowes User ForumsPitney Bowes User Forums
Pitney Bowes User Forums
 
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
 
Leveraging Social Communications for Organizational Thought Leadership
Leveraging Social Communications for Organizational Thought LeadershipLeveraging Social Communications for Organizational Thought Leadership
Leveraging Social Communications for Organizational Thought Leadership
 
Google Developer Day 2010 Japan: Android でリアルタイムゲームを開発する方法: リベンジ (クリス プルエット)
Google Developer Day 2010 Japan: Android でリアルタイムゲームを開発する方法: リベンジ (クリス プルエット)Google Developer Day 2010 Japan: Android でリアルタイムゲームを開発する方法: リベンジ (クリス プルエット)
Google Developer Day 2010 Japan: Android でリアルタイムゲームを開発する方法: リベンジ (クリス プルエット)
 
Google Developer Day 2010 Japan: 「App Engine 開発者コミュニティ「appengine ja night」とフレ...
Google Developer Day 2010 Japan: 「App Engine 開発者コミュニティ「appengine ja night」とフレ...Google Developer Day 2010 Japan: 「App Engine 開発者コミュニティ「appengine ja night」とフレ...
Google Developer Day 2010 Japan: 「App Engine 開発者コミュニティ「appengine ja night」とフレ...
 
Google Developer Day 2010 Japan: 新 SocialWeb: 全てはオープンスタンダードの元に (ティモシー ジョーダン)
Google Developer Day 2010 Japan: 新 SocialWeb: 全てはオープンスタンダードの元に (ティモシー ジョーダン)Google Developer Day 2010 Japan: 新 SocialWeb: 全てはオープンスタンダードの元に (ティモシー ジョーダン)
Google Developer Day 2010 Japan: 新 SocialWeb: 全てはオープンスタンダードの元に (ティモシー ジョーダン)
 
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
Google Developer DAy 2010 Japan: HTML5 についての最新情報 (マイク スミス)
 
Google Developer Day 2010 Japan: HTML5 とウェブサイトデザイン (矢倉 眞隆)
Google Developer Day 2010 Japan: HTML5 とウェブサイトデザイン (矢倉 眞隆)Google Developer Day 2010 Japan: HTML5 とウェブサイトデザイン (矢倉 眞隆)
Google Developer Day 2010 Japan: HTML5 とウェブサイトデザイン (矢倉 眞隆)
 
Google Developer Day 2010 Japan: Android や iPhone で活用する Maps API のモバイル端末向け新機能...
Google Developer Day 2010 Japan: Android や iPhone で活用する Maps API のモバイル端末向け新機能...Google Developer Day 2010 Japan: Android や iPhone で活用する Maps API のモバイル端末向け新機能...
Google Developer Day 2010 Japan: Android や iPhone で活用する Maps API のモバイル端末向け新機能...
 
20130316 プログラミング言語Go
20130316 プログラミング言語Go20130316 プログラミング言語Go
20130316 プログラミング言語Go
 

Similar to Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏)

速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-
Kazunari Hara
 
Write good parser in perl
Write good parser in perlWrite good parser in perl
Write good parser in perl
Jiro Nishiguchi
 
2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」
2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」
2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」
Hiro H.
 
Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34
Appresso Engineering Team
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
Akira Takahashi
 
TypeScript 1.0 オーバービュー
TypeScript 1.0 オーバービューTypeScript 1.0 オーバービュー
TypeScript 1.0 オーバービュー
Akira Inoue
 

Similar to Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏) (20)

Python standard 2022 Spring
Python standard 2022 SpringPython standard 2022 Spring
Python standard 2022 Spring
 
ラズパイでデバイスドライバを作ってみた。
ラズパイでデバイスドライバを作ってみた。ラズパイでデバイスドライバを作ってみた。
ラズパイでデバイスドライバを作ってみた。
 
F#のすすめ
F#のすすめF#のすすめ
F#のすすめ
 
Swiftおさらい
SwiftおさらいSwiftおさらい
Swiftおさらい
 
速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-速くなければスマフォじゃない - インターンバージョン-
速くなければスマフォじゃない - インターンバージョン-
 
Write good parser in perl
Write good parser in perlWrite good parser in perl
Write good parser in perl
 
C-langage
C-langageC-langage
C-langage
 
Rの高速化
Rの高速化Rの高速化
Rの高速化
 
たのしい関数型
たのしい関数型たのしい関数型
たのしい関数型
 
2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」
2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」
2011.12.10 関数型都市忘年会 発表資料「最近書いた、関数型言語と関連する?C++プログラムの紹介」
 
TypeScript と Visual Studio Code
TypeScript と Visual Studio CodeTypeScript と Visual Studio Code
TypeScript と Visual Studio Code
 
Subprocess no susume
Subprocess no susumeSubprocess no susume
Subprocess no susume
 
20141128 iOSチーム勉強会 My Sweet Swift
20141128 iOSチーム勉強会 My Sweet Swift20141128 iOSチーム勉強会 My Sweet Swift
20141128 iOSチーム勉強会 My Sweet Swift
 
Map
MapMap
Map
 
Rust使ってみた
Rust使ってみたRust使ってみた
Rust使ってみた
 
Java x Groovy: improve your java development life
Java x Groovy: improve your java development lifeJava x Groovy: improve your java development life
Java x Groovy: improve your java development life
 
Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34Effective java 輪読会 第6章 項目32-34
Effective java 輪読会 第6章 項目32-34
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
 
姫路IT系勉強会 Vol.11 第0回L-1グランプリ bash
姫路IT系勉強会 Vol.11 第0回L-1グランプリ bash姫路IT系勉強会 Vol.11 第0回L-1グランプリ bash
姫路IT系勉強会 Vol.11 第0回L-1グランプリ bash
 
TypeScript 1.0 オーバービュー
TypeScript 1.0 オーバービューTypeScript 1.0 オーバービュー
TypeScript 1.0 オーバービュー
 

Google Developer Day 2010 Japan: プログラミング言語 Go (鵜飼 文敏)