# 安装 ## build from source ```bash yum -y install make automake gcc gcc-c++ wget; ``` ```bash wget https://dl.google.com/go/go1.4.3.src.tar.gz tar -zxf go1.4.3.src.tar.gz; cd /data/go1.4/go/src; ./all.bash; ``` ```bash wget https://dl.google.com/go/go1.10.3.src.tar.gz tar -zxf go1.10.3.src.tar.gz cd /data/go/src export GOROOT_BOOTSTRAP=/data/go1.4/go ./all.bash; ``` ## 官方的binary ```bash wget https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz ``` ## 设置环境变量 ```bash export GOROOT=/data/sdk/go export GOPATH=/data/sdk/gopath export PATH=$PATH:/data/sdk/go/bin:/data/sdk/gopath/bin ``` ```bash go version ``` # 语言 ## main函数 ```go package main import ( "fmt" ) func main() { //coding fmt.Println("helloworld") } ``` ## Import包 ```go //相对路径 import "./model" //绝对路径 import "shorturl/model" //.操作(省略前缀的包名) import( . "fmt" ) ////fmt.Println("我爱北京天安门") ---> Println("我爱北京天安门") //_操作 import ( "database/sql" _ "github.com/ziutek/mymysql/godrv" ) ////仅执行该包的 init() 方法 //别名操作 import( f "fmt" ) ////fmt.Println("我爱北京天安门") ---> f.Println("我爱北京天安门") ``` ## 变量操作 ```go //声明 var x, y int var ( a int b bool ) //赋值 var a = "你好" var b string = "测试哦" var c bool d := "hello" //值类型和引用类型 var int a = 2; var int *b;//定义一个整形指针 b = &a;//给指针赋值,使指针指向a的地址 ``` ## 结构体和接口 ```go //结构体 type Service struct { //定义成员属性 } //结构体继承(组合实现) type Service2 struct{ Service } //结构体方法(声明receiver) //receiver可以是指针也可是值 //指针接收者,(p *Service) 为 指针接收者 func (p *Service) sayHi(req string) (res string,e error){ return "hi " + req +" !" ,nil; } //值接收者,(p Service) 为 值接收者 func (p Service) sayHi(req string) (res string,e error){ return "hi " + req +" !" ,nil; } //结构体方法重载 //不允许同名方法,支持方法不定参数。没有构造函数。 //接口 type Ser interface{ //定义成员函数 } //实例化结构体 x := &Service{} y := new(Service) var z = Service{} //使用别名为none-local类型增加新方法 type myInt int func(a myInt) add(b myInt) myInt { return a + b; } j :=myInt(5); k :=myInt(6); sum :=j.add(k); fmt.Println("Sum is",sum); //type //定义结构体 type Pojo struct{} //定义接口 type MyInterface interface{} //定义类型等价(类型重命名) type myInt int //定义函数类型 type handler func(name string) int ``` ## Array、Map和List ```go //Array(定长数组) ary := [5]int{7, 77, 88, 13, 14} //Slice(不定长数组) m := make([]int, 0) //初始长度为0 m = append(m, 5) //增加一个 var n []int = []int{1, 2, 3, 4} //Map(键值对) //初始化字典 colors := map[string]string{ "AliceBlue": "#f0f8ff", "Coral": "#ff7F50", "DarkGray": "#a9a9a9", "ForestGreen": "#228b22", } // 修改字典和添加字典 colors["newKey"] = "newValue" if _, ok := colors["Coral"]; ok { fmt.Println("存在Coral") } //根据key删除 delete(colors, "Coral") if _, ok := colors["Coral"]; !ok { fmt.Println("不存在Coral") } //遍历字典 for key, value := range colors { fmt.Printf("Key: %s Value: %s\n", key, value) } //list(链表) l := list.New() for _, value := range colors { l.PushBack(value) } for e := l.Front(); e != nil; e = e.Next() { fmt.Printf("Value: %s\n", e.Value) } ``` ## 类型转换 ```go i, _ := strconv.Atoi("255") //string to int; str := strconv.Itoa(255) //int to string vec := []byte("你好") str1 := "你好" str1P := &str1 str2 := *str1P fmt.Println(i) fmt.Println(str) fmt.Println(string(vec)) fmt.Println(str1) fmt.Println(str1P) fmt.Println(str2) fmt.Println(unsafe.Pointer(str1P)) ``` ## goroutine和channel ### process、thread ```bash Process Identifier,即PID fork函数、子进程 创建开销 ``` ### coroutine、subroutine # 工程 ## 包依赖 ```bash go get 参数说明: -d 只下载不安装 -f 当传了-u参数有效,强制get -u不去验证包has been checked out,This can be useful if the source is a local fork of the original. -fix 在获取源码之后先运行fix,然后再去做其他的事情 -t 同时也下载需要为运行测试所需要的包 -u 强制使用网络去更新包和它的依赖包 -v enables verbose progress and debug output. ``` ```bash export http_proxy=http://127.0.0.1:8118 export https_proxy=http://127.0.0.1:8118 go get -u -v golang.org/x/tools/cmd/guru go get -u -v golang.org/x/tools/cmd/gorename go get -u -v github.com/nsf/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/golang/lint/golint go get -u -v github.com/lukehoban/go-find-references go get -u -v github.com/lukehoban/go-outline go get -u -v github.com/tpng/gopkgs go get -u -v github.com/newhook/go-symbols go get -u -v github.com/derekparker/delve/cmd/dlv go get -u -v github.com/acroca/go-symbols go get -u -v sourcegraph.com/sqs/goreturns go get -u -v github.com/fatih/gomodifytags go get -v -u github.com/go-sql-driver/mysql go get -v -u github.com/alecthomas/log4go go get -v -u github.com/lib/pq go get -v -u github.com/go-xorm/xorm go get -v -u github.com/gin-gonic/gin go get -v -u github.com/tommy351/gin-sessions go get -v -u github.com/spf13/cobra go get -v -u github.com/spf13/pflag go get -v -u github.com/spf13/viper go get -v -u github.com/json-iterator/go go get -v -u github.com/gobuffalo/envy go get -v -u github.com/jmoiron/sqlx go get -u github.com/kataras/iris go get -u github.com/Masterminds/sprig GOPROXY=https://mirrors.aliyun.com/goproxy/ go get -u -v k8s.io/klog GOPROXY=https://mirrors.aliyun.com/goproxy/ go get -v -d github.com/stamblerre/gocode GOPROXY=https://mirrors.aliyun.com/goproxy/ go get -u -v golang.org/x/tools/gopls # https://github.com/julienschmidt/httprouter # https://github.com/mholt/archiver # https://github.com/stretchr/testify # https://github.com/didip/tollbooth # https://gobuffalo.io # https://github.com/mitchellh/gox # https://github.com/hashicorp/serf # https://github.com/Qsnh/goa # https://github.com/b3log/pipe # https://github.com/eiblog/eiblog # https://github.com/go-resty/resty # https://github.com/jeevatkm/go-model ``` * [PackageManagementTools](https://github.com/golang/go/wiki/PackageManagementTools) ## 编译运行 ```bash go build ``` ## 构建 ## web框架 ## 性能分析pprof ## 其他 ```bash mkdir -p quickstart/src/project touch quickstart/src/project/main.go touch quickstart/build.sh ``` ```bash #!/usr/bin/env bash #CURDIR=`pwd` echo `pwd` source /etc/profile echo $GOROOT echo $GOPATH CURDIR=$(dirname "$(readlink -fn "$0")") #project_name="${CURDIR##*/}" echo $CURDIR #echo $project_name; OLDGOPATH="$GOPATH" export GOPATH="$CURDIR" echo $GOPATH #gofmt -w src go build project go install project export GOPATH="$OLDGOPATH" ``` ```bash cd quickstart ./build.sh ``` ## workspace ```bash It is recommend to put your source code in this structure: gopath <-- the folder where environment variable GOPATH is set to └─ src ├─ github.com │ └─ saeidakbari │ ├─ router │ │ └─ router.go <-- package router │ └─ project │ └─ main.go <-- your main package └─ others Then you can import router package in main.go import ( // ...others "github.com/saeidakbari/router" ) For more information please check https://golang.org/doc/code.html#Workspaces ``` # 参考 * [Go tools & GitLab — how to do Continuous Integration like a boss](https://medium.com/pantomath/go-tools-gitlab-how-to-do-continuous-integration-like-a-boss-941a3a9ad0b6)