幾種語言原生開發環境構建之--Go語言

go安裝

安裝gvm版本管理工具

$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
$ gvm install go1.7
$ go version

安裝構建工具

安裝gb工具

$ go get github.com/constabulary/gb/...
$ mkdir src && mkdir src/someuser && mkdir src/someuser/gofirst  # someuser是用戶名稱, gofirst是項目名稱

編寫代碼

  • 源碼
//vi   src/someuser/gofirst/main.go  源碼
package main

import (
	"fmt"
	"github.com/tabalt/gracehttp"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "hello world")
	})

	err := gracehttp.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println(err)
	}
}
  • 測試代碼
//vi   src/someuser/gofirst/main_test.go  測試代碼
package main

import (
	"errors"
	"testing"
 )

func Division(a, b float64) (float64, error) {
	if b == 0 {
		return 0, errors.New("除數不能爲0")
	}

	return a / b, nil
}
func Test_Division_1(t *testing.T) {
	if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
		t.Error("除法函數測試沒經過") // 若是不是如預期的那麼就報錯
	} else {
		t.Log("第一個測試經過了") //記錄一些你指望記錄的信息
	}
}

安裝glide包管理工具

$ curl https://glide.sh/get | sh

項目構建

  • 目錄結構
|-bin
   |-pkg
   |---linux-386
   |-----github.com
   |-------tabalt
   |-----wooz
   |-------http
   |---------vendor
   |-----------github.com
   |-------------tabalt
   |---------------gracehttp
   |-src
   |---wooz
   |-----http
   |-----somes
   |-------test
   |-vendor
   |---github.com
   |-----tabalt
   |-------gracehttp
   |---------gracehttpdemo
   |---src
  • 構建
$ glide init #初始化依賴到glide.yaml文件,並get依賴到vendor目錄
$ glide   --debug up    #更新依賴
$ mkdir vendor/src  
$ mv vendor/g* vendor/src/
$ gb build #構建
$ gb test  -v # 測試或則 go  test src/test/*
  • 配置文件glide.yaml
package: .
import:
- package: github.com/tabalt/gracehttp
testImport:
- package: github.com/smartystreets/goconvey
  version: ^1.6.2
  subpackages:
  - convey
ignore:
   - wooz/somes     #此處忽略沒有git管理的本地包名,讓glide不用去github獲取代碼

項目源代碼

相關文章
相關標籤/搜索