happy go programming part 1

92
Happy Go Programming PART I 林佑安 c9s

Upload: yo-an-lin

Post on 28-Jan-2015

110 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Happy Go Programming Part 1

Happy Go Programming

PART I

林佑安c9s

Page 2: Happy Go Programming Part 1

Intro

• was the GitHub Top 1 Contributor ( http://git.io/top )

• 3+ years Perl/VimL programming

• 3+ years PHP programming

• 2+ years Go programming

Page 3: Happy Go Programming Part 1

Go?

Page 4: Happy Go Programming Part 1

Ken Thompson

Rob Pike

Page 5: Happy Go Programming Part 1

Started since 2007

Page 6: Happy Go Programming Part 1

Announced in November 2009

Page 7: Happy Go Programming Part 1

Used in some of Google’s production system

Page 8: Happy Go Programming Part 1
Page 9: Happy Go Programming Part 1

What’s Go• Statically-Typed Language 靜態型別編譯語⾔言

• Built-in Concurrency 內建並發性⽀支持

• Statically-linked Native Binary 靜態連結原⽣生⼆二進位執⾏行檔

• Fast compilation times 極快速的編譯

• Remote package management 內建外部套件管理

• Garbage Collection 使⽤用垃圾收集器

• Safety 安全 (race conditions, type and memory safety for multithreaded program)

Page 10: Happy Go Programming Part 1

About The Language• Concise variable declaration 簡潔的變數定義

• Type Inference 型別推導

• Easy to use 簡易使⽤用

• Composition instead of Inheritance.

• Use Interface 使⽤用介⾯面取代 is-a 繼承

• multiple return value 多回傳值函數

Page 11: Happy Go Programming Part 1

About The Tools• 語⾔言規格的修改,可透過 go fix 來將舊版語法或程式碼做⾃自動修正

• 編碼⾵風格可透過 go fmt 來統⼀一所有格式,以及⾃自動去除空⽩白⾏行、換⾏行、縮排等等。 且可以⽤用 grammar 的⽅方式來描述語法。

• 內建 profiling tools 且⽀支持 Goole pprof ,可直接在 http server 上開 profiler API,利⽤用命令列⼯工具遠端取得 CPU, Memory 使⽤用狀況並進⾏行分析,或產⽣生 call graph 等等資料。

• 遠端套件可直接透過 go get 安裝

Page 12: Happy Go Programming Part 1

What’s the difference?那麼有什麼不同呢?

Page 13: Happy Go Programming Part 1

Statically-typed vs Dynamically-typed

Page 14: Happy Go Programming Part 1

Statically-typed vs Dynamically-typed

Page 15: Happy Go Programming Part 1

Statically-typed languages are usually

faster

Page 16: Happy Go Programming Part 1

Benchmark

• Faster than Node.js, Python, Perl, PHP, Ruby

• A bit slower than C, C++ and Java (sometimes faster than Java)

• Low memory footprint (10+ times lower than Java)

Page 17: Happy Go Programming Part 1

Benchmarkhttp://www.techempower.com/benchmarks/

Page 18: Happy Go Programming Part 1

and it compiles fast

Page 19: Happy Go Programming Part 1

But it costs a lot of time

Page 20: Happy Go Programming Part 1

Go solves this problem

Page 21: Happy Go Programming Part 1

Concurrency?

Page 22: Happy Go Programming Part 1

Prefork

Page 23: Happy Go Programming Part 1

Prefork

Page 24: Happy Go Programming Part 1

Prefork

Page 25: Happy Go Programming Part 1

Prefork

• Hard to share data between processes.

• Costs a lot of CPU cycle.

• Copying process is time consuming.

• Waste a lot of memory.

Page 26: Happy Go Programming Part 1

Just use Go Routine

Page 27: Happy Go Programming Part 1

Just use Go Routine

All in one process

Page 28: Happy Go Programming Part 1

Just use Go Routine

• Threaded worker pool

• use pipeline (channel) to communicate

• CSP (Communicating Sequential Processes)

http://golang.org/doc/faq

Page 29: Happy Go Programming Part 1

Easier Deployment

Page 30: Happy Go Programming Part 1

Deployment Pain• Install required packages

• Install application package dependencies

• (dependency hell)

• (incompatible version hell)

• (10 hours later…)

• ok, finally onlined.

Page 31: Happy Go Programming Part 1

You can just build & scp

Page 32: Happy Go Programming Part 1

And it works

Page 33: Happy Go Programming Part 1

Q & A Time

Page 34: Happy Go Programming Part 1

————————

Page 35: Happy Go Programming Part 1

Ready For Production?

Page 36: Happy Go Programming Part 1

Companies using Go• Google

• Sound Cloud

• BCC

• Canonical

• Heroku

• Carbon Games

• Iron.io

• SmugMug

• Bitly

• CloudFlare

Page 37: Happy Go Programming Part 1

Enough Open Sourced Packages?

Page 38: Happy Go Programming Part 1

15,298 packageshttp://godoc.org/-/index

Page 39: Happy Go Programming Part 1

Is it popular?

Page 40: Happy Go Programming Part 1
Page 41: Happy Go Programming Part 1

Supported OS?

Page 42: Happy Go Programming Part 1

Supported OS• Linux

• BSD, OpenBSD

• Windows

• Mac OS

• Plan 9

Page 43: Happy Go Programming Part 1

Architectures?

Page 44: Happy Go Programming Part 1

Architectures

• i386

• amd64

• arm

Page 45: Happy Go Programming Part 1

Q & A Time

Page 46: Happy Go Programming Part 1

————————

Page 47: Happy Go Programming Part 1

What You Will Learn Today

Page 48: Happy Go Programming Part 1

Outline• Preparation

• Installation / Build Go by yourself.

• Hello World Go

• Go commands

• Go environment

• Go packages

• Editor and Environment for Go

Page 49: Happy Go Programming Part 1

Outline• Basic Syntax

• Built-in Types

• Variable Declaration

• Function Declaration

• Function Call

• Package

• Type

• Slice

• Array

• Struct

• GoRoutine

• Channel

Page 50: Happy Go Programming Part 1

Outline• Write something

• Your first command-line application.

• Your first package.

• Build & Install

• Using built-in packages.

• Using external packages.

Page 51: Happy Go Programming Part 1

Installation

• Installation Guide: http://golang.org/doc/install

• Go to https://code.google.com/p/go/downloads

• Get your installer

Page 52: Happy Go Programming Part 1

Installation: Compile From Source• Install Mercurial:

• apt-get install mercurial

• port install mercurial

• brew install mercurial

• Go to http://golang.org/doc/install/source

hg clone -u release https://code.google.com/p/go cd go/src ./all.bash

Page 53: Happy Go Programming Part 1

Environment開發環境

Page 54: Happy Go Programming Part 1

Go IDE• Sublime Text 2

• IntelliJ

• LiteIDE

• Intype

• NetBeans

• Eclipse

• Zeus

http://geekmonkey.org/articles/20-comparison-of-ides-for-google-go

Page 55: Happy Go Programming Part 1

go/misc• misc/vim : generic vim plugin

• misc/emacs : emacs go mode

• misc/git : pre-commit hook (run go fmt before commmit)

• misc/bash : bash completion

• zsh/go : zsh completion

• misc/cgo : cgo examples

Page 56: Happy Go Programming Part 1

vim: gocode

• go completion daemon

• vim omni completion support

• scan code from $GOPATH

Page 57: Happy Go Programming Part 1

Go Environment

• $GOROOT ( defaults to /usr/local/go )

• $GOPATH ( your packages )

• $GOARCH

• $GOOS

Page 58: Happy Go Programming Part 1

$GOPATH

mkdir ~/go export GOPATH=~/go

執⾏行 go get 時,packages 會安裝到 GOPATH 第⼀一個 path 內

Page 59: Happy Go Programming Part 1

$GOPATH

mkdir ~/go/vendor mkdir ~/go/private export GOPATH=~/go/vendor:~/go/private

可利⽤用 $GOPATH 將私⽤用 package 分開

Page 60: Happy Go Programming Part 1

$GOPATHmkdir ~/go export GOPATH=~/go

path description

~/go/src your source code

~/go/pkg compiled packages (*.a)

~/go/bin command-line binary

Page 61: Happy Go Programming Part 1

Hello World$ vim hello.go

package main !import "fmt" !func main() { fmt.Printf("hello, world\n") }

Page 62: Happy Go Programming Part 1

Hello World

$ go run hello.go

$ go build -o hello hello.go $ ./hello

Page 63: Happy Go Programming Part 1

Go commands

• go run 編譯後執⾏行程式 (必須是 main package)

• go build 編譯

• go install 編譯並且安裝

• go get 抓取遠端套件並且編譯安裝

Page 64: Happy Go Programming Part 1

Basic Syntax

Page 65: Happy Go Programming Part 1

Skeleton File

package main !// main application goes from here

package libraryA !// code goes from here

Page 66: Happy Go Programming Part 1

Built-in Type

• int, int8, int16, int32, int64

• uint, uint8, uint16, uint32, uint64

• string

• float32, float64

Page 67: Happy Go Programming Part 1

Variable

foo := 1 !var foo = 1 !var foo int = 1 !bar := “foo bar” var bar = “foo bar” var bar string = “foo bar”

Page 68: Happy Go Programming Part 1

Variable Scope

foo := 1 !{ foo := 2 } !// foo 1

var foo int = 1 !{ var foo int = 2 } !// foo = 1

Page 69: Happy Go Programming Part 1

Variable Scope

foo := 1 !for i := range list { foo := 2 } !// foo 1

var foo int = 1 !for i := range list { var foo int = 2 } !// foo = 1

Page 70: Happy Go Programming Part 1

Function Declaration

func FunctionName() { // do something }

Page 71: Happy Go Programming Part 1

Function Declaration

func FunctionName(x int) int { return x * x }

Page 72: Happy Go Programming Part 1

Function Call

!func main() { x := FunctionName(10) }

Page 73: Happy Go Programming Part 1

Function Call With Package

package main !import “lib1” !func main() { x := lib1.FunctionName(10) }

Page 74: Happy Go Programming Part 1

Multiple Return Value

!file, err := os.Open(“log.txt”) if err != nil { // handle error }

Page 75: Happy Go Programming Part 1

Writing Your First Command-line

Application in Go

Page 76: Happy Go Programming Part 1

Command-line package

• package name should be named “main”

• the directory name is the compiled binary name.

• will be installed to $GOPATH/bin

Page 77: Happy Go Programming Part 1

Writing Your First Go Package

Page 78: Happy Go Programming Part 1

General package

• package name should be the same as directory name.

• located in $GOPATH/src

• will be installed to $GOPATH/pkg

Page 79: Happy Go Programming Part 1

Array

items = […]int{ 1, 2, 3 } !

items = [3]int{ 1, 2, 3 }

Page 80: Happy Go Programming Part 1

Slice

Page 81: Happy Go Programming Part 1

Slice

items = []int{ 1, 2, 3 }

Page 82: Happy Go Programming Part 1

Slice: Appending item

items = []int{ 1, 2, 3 } !

items = append(items, 4)

Page 83: Happy Go Programming Part 1

Slice: Extracting

items = []int{ 1, 2, 3 } !

copiedItems = items[:] subItems = items[1:2]

Page 84: Happy Go Programming Part 1

Array vs Slice

• Arrays are fixed-length. Slices are not.

• Arrays are faster than slices.

Page 85: Happy Go Programming Part 1

Struct

Page 86: Happy Go Programming Part 1

Struct• Exported fields starts with upper case letter.

• Private fields starts with lower case letter.

• Code in the same package can access private fields and private functions.

• External package can not access private fields and private functions.

Page 87: Happy Go Programming Part 1

Interface

Page 88: Happy Go Programming Part 1

Type Checking

Page 89: Happy Go Programming Part 1

Type Switch

Page 90: Happy Go Programming Part 1

Universal Interface

var x = interface{} !func Convert(a interface{}) { // do the conversion} !Convert(Foo{}) Convert(Bar{}) Convert(&Foo{}) Convert(&Bar{})

Page 91: Happy Go Programming Part 1

Thank you

Page 92: Happy Go Programming Part 1

• Contact:

• Twitter: @c9s

• Plurk: @c9s

• GitHub: @c9s

• Facebook: http://facebook.com/yoan.lin

[email protected]