go

What happens if I don't cancel a Context?

柔情痞子 提交于 2021-01-20 15:11:57
问题 I have the following code: func Call(ctx context.Context, payload Payload) (Response, error) { req, err := http.NewRequest(...) // Some code that creates request from payload ctx, cancel = context.withTimeout(ctx, time.Duration(3) * time.Second) defer cancel() return http.DefaultClient.Do(req) } What would happen if I didn't put defer cancel() in there? go vet warned this the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak How will the

Go: multiple len() calls vs performance?

◇◆丶佛笑我妖孽 提交于 2021-01-20 14:53:06
问题 At the moment I am implementing some sorting algorithms. As it's in the nature of algorithms, there are a lot of calls on the length of some arrays/slices using the len() method. Now, given the following code for a (part of) the Mergesort algorithm: for len(left) > 0 || len(right) > 0 { if len(left) > 0 && len(right) > 0 { if left[0] <= right[0] { result = append(result, left[0]) left = left[1:len(left)] } else { result = append(result, right[0]) right = right[1:len(right)] } } else if len

Why these two errors are not equal

和自甴很熟 提交于 2021-01-20 14:00:24
问题 I create a err in my package and compare it with io.EOF, but the == operand is false. But their type and value are same? Why == operand return false? func TestErr(t *testing.T) { err := errors.New("EOF") t.Log(err == io.EOF) t.Logf("io err:%T,%v,%p", io.EOF, io.EOF, io.EOF) t.Logf("my err:%T,%v,%p", err, err, err) } These two error are not equal because their pointers are not equal 回答1: error is an interface. It contains a pointer to the underlying value. The io.EOF is created by: var EOF =

Import local package that's already in public repo

不羁的心 提交于 2021-01-20 12:42:51
问题 I am working on building a web framework in Go. I have the code locally and would like to use that repo in another module to test that everything works without having to create tags and/or push things to remote repos. I have followed the official docs on how to do this, as well as a number of posts elsewhere. However nothing seems to work. What am I doing incorrectly? Package live here locally: ../goworkspace/src/github.com/garrettlove8/goserve Import from other module: ... import ( "fmt" "io

Import local package that's already in public repo

廉价感情. 提交于 2021-01-20 12:40:32
问题 I am working on building a web framework in Go. I have the code locally and would like to use that repo in another module to test that everything works without having to create tags and/or push things to remote repos. I have followed the official docs on how to do this, as well as a number of posts elsewhere. However nothing seems to work. What am I doing incorrectly? Package live here locally: ../goworkspace/src/github.com/garrettlove8/goserve Import from other module: ... import ( "fmt" "io

Import local package that's already in public repo

时光怂恿深爱的人放手 提交于 2021-01-20 12:38:44
问题 I am working on building a web framework in Go. I have the code locally and would like to use that repo in another module to test that everything works without having to create tags and/or push things to remote repos. I have followed the official docs on how to do this, as well as a number of posts elsewhere. However nothing seems to work. What am I doing incorrectly? Package live here locally: ../goworkspace/src/github.com/garrettlove8/goserve Import from other module: ... import ( "fmt" "io

一文读懂 Kubernetes APIServer 原理

梦想与她 提交于 2021-01-20 10:39:01
前言 整个Kubernetes技术体系由声明式API以及Controller构成,而kube-apiserver是Kubernetes的声明式api server,并为其它组件交互提供了桥梁。因此加深对kube-apiserver的理解就显得至关重要了。 整体组件功能 kube-apiserver作为整个Kubernetes集群操作etcd的唯一入口,负责Kubernetes各资源的认证&鉴权,校验以及CRUD等操作,提供RESTful APIs,供其它组件调用: kube-apiserver包含三种APIServer: aggregatorServer :负责处理 apiregistration.k8s.io 组下的APIService资源请求,同时将来自用户的请求拦截转发给aggregated server(AA) kubeAPIServer :负责对请求的一些通用处理,包括:认证、鉴权以及各个内建资源(pod, deployment,service and etc)的REST服务等 apiExtensionsServer :负责CustomResourceDefinition(CRD)apiResources以及apiVersions的注册,同时处理CRD以及相应CustomResource(CR)的REST请求(如果对应CR不能被处理的话则会返回404)

go Module升级依赖以及设置网络代理

与世无争的帅哥 提交于 2021-01-20 09:50:57
Go Module升级依赖和设置代理 go module为官方出的一款依赖管理工具,社区大部分的应用都以及采用这种方式来进行依赖包管理,下面是我日常开发过程中用到的一些命令记录。 设置代理 由于 gfw 的存在,导致国外的好多资源无法访问,golang的一些依赖包 golang.org/x/image 等也无法下周,所以就需要使用代理软件 shadowsocket 来访问,在 ss 中开启本地http端口代理,一般为 1080 端口,然后再终端中设置环境变量 http_proxy https_proxy . set http_proxy = http://127.0.0.1:1080 set https_proxy = http://127.0.0.1:1080 1 2 之后就可以愉快的下载依赖啦~ 常用方法 初始化 进入到项目中,用 go mod init github.com/xxx/xxx 添加依赖 添加依赖: go get github.com/xxx/xxx 添加指定版本: go get github.com/xxx/xxx@v1.6.2 添加指定版本范围: go get github.com/xxxx/xxx@'<v1.6.2' 添加指定commit的版本 git commit: go get github.com/xxxx/xxx@q2516faf3 升级依赖 升级

线性表的顺序表示和实现

萝らか妹 提交于 2021-01-20 09:22:01
1 #include<bits/stdc++.h> // c++万能头文件,写了这个其他头文件不用写 2 using namespace std; // 使用名字空间,你不用管 3 4 #define List_Init_Size 100 5 #define List_Inrement 10 6 #define pr printf 7 8 struct List{ 9 int *elem; // 表的基地址 10 int length; // 当前表长 11 int listsize; // 当前表的容量 12 }; 13 14 // 初始化线性表 15 bool InitList(List & L) 16 { 17 L.elem = ( int *) malloc (List_Init_Size* sizeof ( int )); 18 if (L.elem == NULL) exit(- 1 ); // 如果分配内存失败,就退出程序 19 20 L.length = 0 ; 21 L.listsize = 100 ; 22 return 1 ; 23 } 24 25 // 线性表插入元素 26 bool ListInsert(List &L, int i, int e) 27 { 28 if (i < 1 || i > L.length + 1 ) 29 { 30 puts(

Generating identical random numbers in sequence after time seed? (Running on my machine)

我的未来我决定 提交于 2021-01-20 07:51:29
问题 I'm trying to understand precisely why, when called from an external function, my time seeded random number generator returns sequences of identical numbers. Minimal working example of issue: package main import ( "fmt" "math/rand" "time" ) //Generates random int as function of range func getRand(Range int) int { s1 := rand.NewSource(time.Now().UnixNano()) r1 := rand.New(s1) return r1.Intn(Range) } //Print 100 random ints between 0 and 100 func main() { for i := 0; i < 100; i++ { fmt.Print