Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go - Getting Started

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for nyo nyo
September 14, 2018

Go - Getting Started

Avatar for nyo

nyo

September 14, 2018

Other Decks in Programming

Transcript

  1. 優勢 Introduction for Go • 老爸是 Google • 開源專案 •

    編譯成執行檔,執行性能較好 • 易於開發維護 • function programing (not OOP) • goroutines 的優勢
  2. Tool Introduction for Go • go get • go run

    • go test • go build • go install
  3. Tools for Editor Editor • Auto completion (gocode • Code

    navigation (godef, guru • Format (gofmt, goimport • Lint (golint, gometalinter • Debug (delve
  4. GOPATH Write Go Code • Default ◦ $HOME/go • PATH

    ◦ export PATH=$PATH:$(go env GOPATH)/bin
  5. Strongly Typed Features of Go var i int var c,

    python, java bool var counter int = 5 var message string = "Hello" Variable declaration and initialization var counter = 5 var message = "Hello" func main(){ counter := 5 message := "Hello" } Automatically assign type
  6. Struct Features of Go type Vertex struct { X int

    Y int z int // private } Defined v := Vertex{ X: 1, Y: 2, } v.X = 4 Create instance
  7. Functional Programming Features of Go func add(m, n int) int

    { return m + n } Function type MyInt struct { m int n int } func (i *MyInt) sum() int { return i.m + i.n } Method
  8. Package Features of Go import ( “math/rand" ) rand.Intn(10) Import

    package import ( "github.com/gin-gonic/gin" "github.com/gorilla/websocket" ) gin.New() Import third package
  9. Concurrency: Goroutines and Channel Features of Go func say(s string)

    { fmt.Println(s) } go say("world") Goroutines By default, sends and receives block until the other side is ready. ch := make(chan int) ch <- v // Send v to channel ch. v := <-ch // Receive from ch, and // assign value to v. Channel
  10. Interface Features of Go type Abser interface { Abs() float64

    } Defined Interface type MyFloat float64 func (f MyFloat) Abs() float64 { if f < 0 { return float64(-f) } return float64(f) } Implemented interface