(转) go 汇总, cloud.google.com 无法下载 等解决办法

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我

原文链接:https://blog.csdn.net/wangjunsheng/article/details/81481777

第三方库

google 被墙了,很多相关的库都 go get 不下来。可以用下面的方法曲线下载

cloud.google.com/go/pubsub

对应的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

google.golang.org/api/iterator

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/x/sync/errgroup golang.org/x/oauth2

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.

grpc

https://github.com/grpc/grpc-go.git

genproto https://github.com/google/go-genproto.git

go grpc protobuf 安装 及 用法简单示例

二分查找

// 二分查找找到第一个大于等于 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 类型转换

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)
}

map

	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]

array

a := make([]int, 10, 100) // 第2个参数 len(a) 的值, 第三个 cap(a) 的值
                          // 第三个参数 相当于 c++ 的 reserve() 函数
a = append(a, n)          // 如果 cap(a) 不够,会扩容,好像默认是2倍当前cap(a)扩容
                          // 实测发现 append 效率很高

string 互换 []byte

[]byte("wjs")    // ['w','j','s']
 
string( []byte{'a', 'b', 'c'} ) // "abc"

%v %q %b %p

golang 中fmt用法

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使用新的时间片

相关文章