Slide 1

Slide 1 text

Introduction Go ShuuuMai #04, MAY 30 2018 Yoichiro Shimizu freee k.k. @budougumi0617

Slide 2

Slide 2 text

● 清水 陽一郎 @budougumi0617 ● freee 株式会社 アプリケーションエンジニア ○ Go / Ruby on Rails / .NET(WPF/UWP) / Xamarin.Mac ○ 前職ではC/C++/.NET(WPF) ● Blog ○ https://budougumi0617.github.io/ 自己紹介

Slide 3

Slide 3 text

Today’s contents Goとは 01 Goの特徴 Goを使った開発 Goを始める まとめ 02 03 04 05

Slide 4

Slide 4 text

What is Go? SECTION ONE

Slide 5

Slide 5 text

● 2009年に公開 ○ (v1.0は2012年) ● Google製 ● Gopherくんがマスコット Goとは The Go gopher was designed by Renee French. gopher-front.png was created by Takuya Ueda. 01. What is Go?

Slide 6

Slide 6 text

{ Goのデザインポリシー Each language feature should be easy to understand. 01. What is Go?

Slide 7

Slide 7 text

{ Goのデザインポリシー Go is an open source programming language that enables the production of simple, efficient and reliable software at scale. 01. What is Go?

Slide 8

Slide 8 text

Features SECTION TWO

Slide 9

Slide 9 text

静的型付けのコンパイル言語 ● 暗黙の型変換がない ● 型推論 ● シンプルな文法・言語仕様 ● リッチな標準ライブラリ ● クロスコンパイル・マルチプラットフォーム対応 02. Features

Slide 10

Slide 10 text

シンプルな文法・言語仕様 break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var あとは演算子とプリミティブ型を覚えるだけ。 02. Features

Slide 11

Slide 11 text

リッチな標準ライブラリ package main import ( "fmt" "log" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(":8080", nil)) } 02. Features

Slide 12

Slide 12 text

CSP由来の並行処理 package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } 02. Features

Slide 13

Slide 13 text

In Development SECTION THREE

Slide 14

Slide 14 text

CLIとGo ● クロスコンパイル可能 ○ マルチプラットフォーム対応がラク ● 主要なOSSにもGo製が多い ○ Docker/k8s/fzf/peco/direnv... 03. In Development

Slide 15

Slide 15 text

Web開発とGo ● 1バイナリでリリース可能 ○ Docker/Kubernetesなどと相性が良い ● 多人数開発に向いてる ○ 迷わず読める ○ 迷わず書ける 03. In Development

Slide 16

Slide 16 text

工夫しない工夫 ● Goの「窮屈さ」をどう考えるか ○ reflectionなどで強引に解決 ○ 齟齬がでたりして破綻することが多い 03. In Development

Slide 17

Slide 17 text

Gowayという名のレール 03. In Development func main() { fmt.Println("Hello World") } syntax error: unexpected semicolon or newline before { func main() { fmt.Println("Hello World") } Correct format

Slide 18

Slide 18 text

Gowayという名のレール 03. In Development func main() { fmt .Println("Hello World") } syntax error: unexpected semicolon or newline before { func main() { fmt. Println("Hello World") } Correct format

Slide 19

Slide 19 text

Gowayという名のレール ● 小さく・疎な設計になりやすい言語仕様 ○ interfaceによるダックタイピング ○ 循環参照を許さないimport構造 ● ロジックに集中して コーディング・レビュー出来る ● 窮屈さが質を保証する 03. In Development

Slide 20

Slide 20 text

Table Driven Test 03. In Development func TestFizzBuzz(t *testing.T) { tests := []struct { subject string input int want string }{ {subject: "num", input: 2, want: "2"}, {subject: "multi3", input: 3, want: "Fizz"}, {subject: "multi5", input: 5, want: "Buzz"}, ... } for _, tt := range tests { t.Run(tt.subject, func(t *testing.T) { got, err := FizzBuzz(tt.input) if got != tt.want { t.Errorf("want %d, but %s:", tt.want, got) } } } ● Found bug ○ Add case ○ Start TDD ● Maintainable ● Reusability

Slide 21

Slide 21 text

標準Interfaceとダックタイピング 03. In Development type Writer interface { Write(p []byte) (n int, err error) } // ProxyWriter has Write method type ProxyWriter struct {} func (p *ProxyWriter) Write(p []byte) (int, error) { // Some doing... } func Save(buf []byte, w io.Writer) { // Some doing... } Save(buff, &ProxyWriter{}) ● Goはダックタイピング ● なるべく標準Interfaceを実装する ○ io.Writer ○ io.Reader ○ fmt.Stringer ○ sync.Locker ● OSSも標準interfaceの実装が多い ● ReplaceやOSSの利用がしやすい ● 自分で定義するとしても単一責務の 原則を守ること

Slide 22

Slide 22 text

Start Go SECTION FOUR

Slide 23

Slide 23 text

● A Tour of Go ○ https://tour.golang.org/ ● A Tour of Go日本語版 ○ https://go-tour-jp.appspot.com/ Webでカジュアルに試す 04. Start Go

Slide 24

Slide 24 text

● OSに合ったバイナリを取得する ○ https://golang.org/doc/install ● PATHを通す ○ $ export PATH=$PATH:/usr/local/go/bin Goのインストール方法 04. Start Go

Slide 25

Slide 25 text

● 主要なEditorにプラグインあり ○ Visual Studio Code ○ Atom ○ Vim ○ Emacs ● JetbrainsからIDEも公開 ○ Goland ● (自分はVimで書いてます) Goの開発環境 04. Start Go

Slide 26

Slide 26 text

● delve ○ https://github.com/derekparker/delve ○ GDBライクなデバッガ ● gore ○ https://github.com/motemen/gore ○ irb, iexなどのようなREPL ● richgo ○ https://github.com/kyoh86/richgo ○ go testの結果出力をカラフルに (公式以外の)便利なツール 04. Start Go

Slide 27

Slide 27 text

● golang.tokyo ○ https://golangtokyo.connpass.com/ ● Goビギナー ○ https://go-beginners.connpass.com/ ● Women Who Go ○ https://womenwhogo-tokyo.connpass.com/ ● 横浜Go読書会 ○ https://yokohama-go-reading.connpass.com/ ● GoCon ○ https://gocon.connpass.com/ ● Akiba.go ○ https://akihabarago.connpass.com/ ● kamakura.go ○ https://twitter.com/kamakura_golang 関東のGoの勉強会 04. Start Go

Slide 28

Slide 28 text

● gophers.slack.com ● 以下のURLから参加できます ○ https://gophersinvite.herokuapp.com/ ● 日本語チャンネルあります ○ #tokyo ○ #japan Slack (Global) 04. Start Go

Slide 29

Slide 29 text

Today’s Summary SECTION FIVE

Slide 30

Slide 30 text

● Goはシンプルさを追求した言語 ○ 迷わず書ける ○ 迷わず読める ● Web開発、CLIツールの開発で人気 ○ コンテナー化に向いている ○ マルチプラットフォーム対応がラク ● 興味をもった方はA Tour of Go/そしてインストール ● コミュニティに入れば初心者でも大丈夫 Summaries