这是一个汇总帖,记录go的一些基础
升级到go最新版本
export GOPROXY=https://goproxy.io
基本就都可以下载了
google 被墙了,很多相关的库都 go get 不下来。可以用下面的方法曲线下载
对应的github仓库在这里 https://github.com/googleapis/google-cloud-go
mkdir -p $GOPATH/src/cloud.google.com
cd $GOPATH/src/cloud.google.com
~/go/gopath/src/cloud.google.com $ git clone https://github.com/googleapis/google-cloud-go.git
Cloning into 'google-cloud-go'...
remote: Enumerating objects: 427, done.
remote: Counting objects: 100% (427/427), done.
remote: Compressing objects: 100% (186/186), done.
remote: Total 20434 (delta 266), reused 332 (delta 221), pack-reused 20007
Receiving objects: 100% (20434/20434), 14.46 MiB | 71.00 KiB/s, done.
Resolving deltas: 100% (14029/14029), done.
Checking connectivity... done.
~/go/gopath/src/cloud.google.com $ mv google-cloud-go go
googleapis go 语言版本在这 https://github.com/googleapis/google-api-go-client
~/go/gopath/src/google.golang.org $ git clone https://github.com/googleapis/google-api-go-client.git
Cloning into 'google-api-go-client'...
remote: Enumerating objects: 1495, done.
remote: Counting objects: 100% (1495/1495), done.
remote: Compressing objects: 100% (973/973), done.
remote: Total 72438 (delta 687), reused 1013 (delta 363), pack-reused 70943
Receiving objects: 100% (72438/72438), 132.27 MiB | 642.00 KiB/s, done.
Resolving deltas: 100% (35349/35349), done.
Checking connectivity... done.
~/go/gopath/src/google.golang.org $ mv google-api-go-client/ api
golang.org 的基本都在 https://github.com/golang, 到里面找同名的仓库
详细参考如下
cd $GOPATH/src/golang.org/x/
~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/sync.git
Cloning into 'sync'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 103 (delta 0), reused 1 (delta 0), pack-reused 99
Receiving objects: 100% (103/103), 40.98 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Checking connectivity... done.
~/go/gopath/src/golang.org/x $ git clone https://github.com/golang/oauth2.git
Cloning into 'oauth2'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 1686 (delta 4), reused 3 (delta 3), pack-reused 1681
Receiving objects: 100% (1686/1686), 517.60 KiB | 40.00 KiB/s, done.
Resolving deltas: 100% (1036/1036), done.
Checking connectivity... done.
https://github.com/grpc/grpc-go.git
genproto https://github.com/google/go-genproto.git
// 二分查找找到第一个大于等于 findvalue 数字的索引
a := []int {0,2,4,6,8,10}
findvalue := 5
idx := sort.Search(len(a), func(i int) bool { return a[i] >= findValue})
// findvalue 5 a[idx] = 6
// findvalue 6 a[idx] = 6
interface 有点像c语言的 void*
c语言 go 语言类型转换, 还返回了个 ok , 表示是否转换成功
(string*)p ==> p.(*string)
func setName(i interface{}) {
name, ok := i.(*string)
if !ok {
log.Printf("expected *string, got: %T", i)
return
}
log.Println(*name)
}
m := make(map[string]int)
// 添加
m["a"] = 33
m["wjs"] = 23
// 是否存在
name, ok := m["tianhe"]
if !ok {
log.Print("tianhe not exist in m", m)
} else {
log.Print("m[tianhe] = ", name)
}
// 删除
delete(m, "wjs")
log.Print(m) // map[a:33]
a := make([]int, 10, 100) // 第2个参数 len(a) 的值, 第三个 cap(a) 的值
// 第三个参数 相当于 c++ 的 reserve() 函数
a = append(a, n) // 如果 cap(a) 不够,会扩容,好像默认是2倍当前cap(a)扩容
// 实测发现 append 效率很高
[]byte("wjs") // ['w','j','s']
string( []byte{'a', 'b', 'c'} ) // "abc"
fmt.Sprintf("%+11.2f", 5.126)
gofmt ser.go > ser.go
go test -v
//go:generate stringer -type=CmdType
type CmdType uint8
const (
NodeRegister CmdType = iota // 节点注册
NodeReport // 节点上报数据
ACKOK // 服务器返回ACK OK
ACKSetNewTimeSlice // 服务器ACK,告知Node使用新的时间片
)