That's GO

golang introduction

Problems

  • 使用 C++ 作為 sever 需要額外使用 Java 跟 Python
  • 大量工程師與非常大量程式碼
  • 分散式系統 - threads 效能
  • 許多 compute cluster

Purpose

  • Concurrent
    • https://tour.golang.org/concurrency/1
  • Quickly Compiled (Compare with C/C++)
  • GC

Main Stuff

  • 開放原始碼 (open source)
  • 靜態型別的編譯語言;但語法類似腳本語言 (scripting language)
  • 跨平台 (cross-platform)
  • 內建全自動的垃圾回收 (garbage collection),可手動調整
  • 內建平行處理 (concurrency)
  • 輕量級物件 (object) 系統
  • 程式風格強制統一
  • 快速編譯
  • 內建開發相關工具
  • 豐富的標準函式庫
  • 成長中的社群資源

Couple Features

  • godoc
  • go test
  • cross compile
    • golang-crosscompile
// Compile file
go build

// Test (*_test.go)
go test 

// formatting
go fmt

// install package
go get

// analysis possible bug
go vert

// quickly build and run 
go run

// show go document
godoc

Code readability or efficiency

Declaretion

// variable v1
var name string 

name = "Stanney"

// variable v2

name := "Stanney"

// const

const PI = 3.14159


var

const

func

type

Data Types

  • 基本型別 (basic types)
    • ​整數 (Eg. int8)
    • 浮點數 (Eg. float32)
    • 複數 (Eg. complex64)
  • 聚合型別 (aggregate types)
    • ​array
    • struct
  • 參考型別 (reference Types)
    • ​pointer
    • slice
    • map
    • function
    • channels
  • 介面型別 (interface types)

Struct

package main
 
import (
  "fmt"
)
 
type Point struct {
  x float64
  y float64
}
 
func main() {
  p := Point{x: 3.0, y: 4.0}
 
  fmt.Println(p.x)
  fmt.Println(p.y)
}

Array & Slice

package main
 
import "log"
 
func main() {
  var langs [4]string
 
  langs[0] = "Go"
  langs[1] = "Python"
  langs[2] = "Ruby"
  langs[3] = "PHP"
 
  if !(langs[0] == "Go") {
    log.Fatal("Wrong string")
  }
}
package main
 
import "fmt"
 
func main() {
  langs := []string{"Go", "Python", "Ruby", "PHP"}
 
  for _, e := range langs {
    fmt.Println(e)
  }
}

Map

package main
 
import (
    "fmt"
)
 
func main() {
    m := make(map[string]string)
 
    m["Go"] = "Beego"
    m["Python"] = "Django"
    m["Ruby"] = "Rails"
    m["PHP"] = "Laravel"
 
    for i := 0; i < 10; i++ {
        for k, v := range m {
            fmt.Println(fmt.Sprintf("%s: %s", k, v))
        }
 
        fmt.Println("")
    }
}

Pointer

package main
 
import (
    "fmt"
)
 
type Point struct {
    x float64
    y float64
}
 
func main() {
    p := new(Point)
 
    p.x = 3.0
    p.y = 4.0
 
    fmt.Println(fmt.Sprintf("(%.2f, %.2f)", p.x, p.y))
}

Class ?

package main
 
import (
    "log"
    "math"
)
 
type Point struct {
    x float64
    y float64
}
 
func NewPoint(x float64, y float64) *Point {
    p := new(Point)
 
    p.SetX(x)
    p.SetY(y)
 
    return p
}
 
func (p *Point) X() float64 {
    return p.x
}
 
func (p *Point) Y() float64 {
    return p.y
}
 
func (p *Point) SetX(x float64) {
    p.x = x
}
 
func (p *Point) SetY(y float64) {
    p.y = y
}
 
func Dist(p1 *Point, p2 *Point) float64 {
    xSqr := math.Pow(p1.X()-p2.X(), 2)
    ySqr := math.Pow(p1.Y()-p2.Y(), 2)
 
    return math.Sqrt(xSqr + ySqr)
}
 
func main() {
    p1 := NewPoint(0, 0)
    p2 := NewPoint(3.0, 4.0)
 
    if !(Dist(p1, p2) == 5.0) {
        log.Fatal("Wrong value")
    }
}

Install Go

  • Download pkg: https://golang.org/dl/
    or brew install go
  • 透過 pkg 的需要額外設定以下至 bashrc or zshrc
#GOROOT
export GOROOT=/usr/local/go
#GOPATH
export GOPATH=$HOME/Documents/go_workspace
#GOROOT bin
export PATH=$PATH:$GOROOT/bin

#GOPATH bin
export PATH=$PATH:$GOPATH/bin

Hello, World

  • 在 go_workspace 下面新增 src/hello 資料夾
  • 新增一個 hello.go,並打上下方的 code
  • 在 src/hello 下方執行 go build,便會產生一個 .hello 執行檔
  • ./hello 便會出現一個 "hello, world"
package main

import "fmt"

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

Go Restful

  • 原生有 net/http 函式庫
  • 下方程式碼便可運行在 localhost:8080 上
package main

import (
  "fmt" // printf
  "html"
  "log"
  "net/http" // Package http provides HTTP client and server implementations
)

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
  })

  log.Fatal(http.ListenAndServe(":8080", nil))
}

Demo 

step by step refactor

Golang Introduction

By Stanney Yen

Golang Introduction

  • 269