草庐IT

go-import

全部标签

go - 如何通过 Golang 中的函数传递接口(interface)指针?

我在测试golang功能时遇到了这个概念,我可以将接口(interface)的指针用作接口(interface)本身。在下面的代码中,如何确保one的值更改为random。packagemainimport("fmt")funcsome(checkinterface{}){check="random"}funcmain(){varone*interface{}some(one)fmt.Println(one)}具体来说,我需要将接口(interface)指针传递给接受接口(interface)作为参数的函数的方法。谢谢! 最佳答案

Go:将许多慢速 API 查询引导到单个 SQL 事务中

我想知道下面的惯用方法是什么。我有N个慢速API查询和一个数据库连接,我想要一个缓冲channel,响应将来自该channel,以及一个我将用来写入数据的数据库事务。我只能想出以下化妆示例的信号量:funcmyFunc(){//10concurrentAPIcallssem:=make(chanbool,10)//AconcurrentsafemapasbuffervarmyMapMyConcurrentMapfori:=0;i我几乎可以肯定有更简单、更干净、更合适的解决方案,但对我来说似乎很难掌握。编辑:好吧,我提出了以下解决方案,这样我就不需要缓冲区映射,所以一旦数据到达respc

go - 将命名类型转换为未命名类型

假设我有一个UnnamedTypes结构:typeUnnamedTypesstruct{i[]intf[]float64}以及结构中的一些命名类型:typeI[]inttypeF[]float64typeNamedTypesstruct{iIfF}将NamedTypes结构分配给UnnamedTypes结构的最简单方法是什么?funcmain(){varuUnnamedTypesvarnNamedTypesu.i=[]int{1,2}u.f=[]float64{2,3}n.i=[]int{2,3}n.f=[]float64{4,5}u=UnnamedTypes(n)}失败,无法将n(类

sockets - 非常偶发的 Go HTTP 错误 : multiple response. WriteHeader 调用

我写了Kanali这是一个开源KubernetesIngress/API管理工具,对于大约1/200k请求,我收到以下fatalerror:2017/08/1612:40:57http:multipleresponse.WriteHeadercalls{"level":"error","method":"GET","msg":"unknownerror","time":"2017-08-16T12:40:57Z","uri":"/ommitted/path"}{"level":"fatal","msg":"writetcp4192.168.2.160:8443-\u003e192.16

go - golang中指针内容的差异

考虑以下结构和指针引用示例的代码。packagemainimport("fmt""sync")typemyStructstruct{mut*sync.RWMutex}typearrayOfMyStructstruct{structList[]myStruct}funcmain(){k:=&sync.RWMutex{}myStructInstance:=myStruct{k}fmt.Printf("%p",&k)//address1->addressofkfmt.Println("")fmt.Printf("%p",&*k)//address2->addressofwhatkpoints

go - Go 中的 int 数据类型

我是Go语言的新手,正在尝试GO中的几个示例。在GO中int不是关键字,所以我声明了一个名称为int的变量。packagemainimport"fmt"funcmain(){varintint=8fmt.Println(int)varnumberint=10fmt.Println(number)}现在,当我构建这段代码时,出现以下错误:[dev@gotest]$gobuildvariables.go#command-line-arguments./variables.go:8:intisnotatype我试图理解为什么会出现这种情况,以及varintint做了什么使得int成为不可用的

go - 如何在 golang 网络服务器中使用 prometheus 监控请求成本时间

我有几个url可以访问,我想用prometheus监控每个请求的成本时间。但我不知道使用哪种指标来收集数据。有什么帮助吗?这是演示代码:packagemainimport("github.com/prometheus/client_golang/prometheus""io/ioutil""net/http""fmt""time")var(resTime=prometheus.NewSummaryVec(prometheus.SummaryOpts{Name:"response_time",Help:"costtimeperrequest",},[]string{"costTime"}

go - 如何调用父方法并将扩展父结构的任何子结构作为 Golang 中的参数传递

我还在学习Golang,想请教一下。是否有可能做这样的事情并将任何其他child传递给扩展父结构的PMethod?typeParentstruct{PAttributestring}func(p*Parent)PMethod(c*Child){fmt.Println("thisisparentAttribute:"+p.PAttribute)fmt.Println("thisischildAttribute:"+c.CAttribute)}typeChildstruct{ParentCAttributestring}typeChild2struct{ParentCAttributest

go - 重定向到 Go 中的页面问题

这个问题在这里已经有了答案:multipleresponse.WriteHeadercallsinreallysimpleexample?(6个答案)Golanghttp:multipleresponse.WriteHeadercalls(1个回答)关闭5年前。我正在通过go服务index.html。但是,根据将通过页面发送的某些参数,go应该成功重定向到不同的页面。尝试执行代码时出现以下错误。http:多次响应.WriteHeader调用funcmain(){http.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){h

go - 从 slice 中删除所有出现的相同元素

我有以下代码:funcTestRemoveElement(t*testing.T){nums:=[]int{3,2,2,3}result:=removeElement(nums,3)ifresult!=2{t.Errorf("Expected2,butitwas%dinstead.",result)}}funcremoveElement(nums[]int,valint)int{fori,v:=rangenums{ifv==val{nums=append(nums[:i],nums[i+1:]...)}}returnlen(nums)}根据answer,if语句中的语句是替换slice