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

项目源代码

转载于:https://my.oschina.net/jackywyz/blog/729571

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值