2021
06-04
06-04
golang 函数返回chan类型的操作
在阅读kafka的golang客户端代码sarama-cluster时,遇到了如下一段代码://Messagesreturnsthereadchannelforthemessagesthatarereturnedby//thebroker.////ThischannelwillonlyreturnifConfig.Group.Modeoptionissetto//ConsumerModeMultiplex(default).func(c*Consumer)Messages()<-chan*sarama.ConsumerMessage{returnc.messages}对于代码中的<-chan*sarama.ConsumerMessage产生了疑问,...
继续阅读 >
在使用Context.ResponseWriter中的Set/WriteHeader/Write这三个方法时,使用顺序必须如下所示,否则会出现某一设置不生效的情况。ctx.ResponseWriter.Header().Set("Content-type","application/text")ctx.ResponseWriter.WriteHeader(403)ctx.ResponseWriter.Write([]byte(resp))如1:ctx.ResponseWriter.Header().Set("Content-type","application/text")ctx.ResponseWriter.Write([]byte(resp))ctx.ResponseWriter.WriteH...
超时建立连接主要就2函数Dail和DialContext。//Dialcreatesaclientconnectiontothegiventarget.funcDial(targetstring,opts...DialOption)(*ClientConn,error){returnDialContext(context.Background(),target,opts...)}funcDialContext(ctxcontext.Context,targetstring,opts...DialOption)(conn*ClientConn,errerror){...}DialContext太长了不帖了.看Dial实际上也是调用DialContext来实现的....
Golang超大文件读取的两个方案流处理方式分片处理去年的面试中我被问到超大文件你怎么处理,这个问题确实当时没多想,回来之后仔细研究和讨论了下这个问题,对大文件读取做了一个分析比如我们有一个log文件,运行了几年,有100G之大。按照我们之前的操作可能代码会这样写:funcReadFile(filePathstring)[]byte{content,err:=ioutil.ReadFile(filePath)iferr!=nil{log.Println("Readerror")}ret...
post请求常用的几种方式,记录一下funchttpPost(){resp,err:=http.Post("https://www.abcd123.top/api/v1/login","application/x-www-form-urlencoded",strings.NewReader("username=test&password=ab123123"))iferr!=nil{fmt.Println(err)}deferresp.Body.Close()body,err:=ioutil.ReadAll(resp.Body)iferr!=nil{//handleerror}fmt.Println(...