几种语言原生开发环境构建之--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获取代码

项目源代码

相关文章
相关标签/搜索