安装

build from source

1
yum -y install make automake gcc gcc-c++ wget;
1
2
3
4
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;
1
2
3
4
5
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

1
wget https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz

设置环境变量

1
2
3
export GOROOT=/data/sdk/go
export GOPATH=/data/sdk/gopath
export PATH=$PATH:/data/sdk/go/bin:/data/sdk/gopath/bin
1
go version

语言

main函数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import (
	"fmt"
)

func main() {
	//coding
    fmt.Println("helloworld")
}

Import包

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//相对路径
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("我爱北京天安门")

变量操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
//声明

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的地址

结构体和接口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//结构体
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//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)
}

类型转换

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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

1
2
3
Process Identifier,即PID
fork函数、子进程
创建开销

coroutine、subroutine

工程

包依赖

1
2
3
4
5
6
7
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.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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/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/gin-gonic/gin

编译运行

1
go build

构建

web框架

性能分析pprof

其他

1
2
3
mkdir -p quickstart/src/project
touch quickstart/src/project/main.go
touch quickstart/build.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/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"
1
2
cd quickstart
./build.sh

参考