草庐IT

is-there-an-environment-variable-

全部标签

go - 传播时 "Cannot use variable of type []struct as []interface"

这个问题在这里已经有了答案:sliceofstruct!=sliceofinterfaceitimplements?(6个答案)关闭8个月前。原型(prototype)函数functest(i...interface{}){//Codehere}预期用途typefoostruct{//Fields}foos:=[]foo{//foo1,foo2...}test(foos...)//ERRORtest(foos[1],foos[2],...)//OK错误cannotusefoos(variableoftype[]foos)as[]interface{}valueinargumenttot

variables - Golang 的 init() 函数中的包范围变量赋值

我想在一些Go代码中初始化一个包范围的变量以连接到数据库,但我一直收到nil指针异常,很明显赋值没有正确发生。这会引发错误:packagemainimport("fmt""database/sql"_"github.com/lib/pq")varconnection*sql.DBfuncinit(){connectionString:="host=172.17.0.3dbname=postgresuser=postgrespassword=postgresport=5432sslmode=disable"connection,err:=sql.Open("postgres",conne

for-loop - 遍历 channel 时出现错误 "too many variables in range"

我在这里有点迷路了,我试图让一个goroutine添加到数组中,并让另一个goroutine从中读取,我怀疑这有点接近我下面的内容,但我需要尝试一下等待()。但是,我收到错误prog.go:19:14:toomanyvariablesinrange,第19行是for_,v:=rangec{我在网上找不到这个问题的答案,我在这里做什么或不做什么?packagemainimport("fmt"//"time""sync")funchello(wg*sync.WaitGroup,s[]int,cchanint){for_,v:=ranges{c 最佳答案

variables - golang 变量替换

如何在go中使用变量替换?例如我有以下代码:debug_level:="Info"log.Info("DebugTest")我可以为日志函数使用变量debug_level而不是直接传递参数吗?像这样的东西:debug_level:="Info"log.${debug_level}("DebugTest")谢谢。 最佳答案 Go是一种静态类型语言,如果您通过名称调用函数或方法,编译器无法检查您提供的参数是否与函数的签名匹配。改为使用函数变量:当前保存方法或函数的变量名称可以是函数类型的变量,保存函数或方法值.假设我们有以下日志功能:f

list - 为什么将列表整数元素转换为字符串时得到 "interface conversion: interface is int32"?

Go新手...我编写了一个程序来删除存储在列表中的重复整数。当我为removeDuplicates函数运行以下测试时,我收到以下指向此行的错误:linked_test中的testString+=strconv.Itoa(e.Value.(int))。去吧。为什么会这样,我该如何解决?我将整数存储在testList中并使用e.Value获取它们并使用.(int)进行类型转换。panic:interfaceconversion:interfaceisint32,notint[recovered]panic:interfaceconversion:interfaceisint32,notin

去教程: what is the & doing in this line?

我正在做go教程,我对这个练习有疑问...https://tour.golang.org/moretypes/5我之前只在基本的C代码中简单地使用过指针和地址。我的理解是p=&Vertex{1,2}//hastype*Vertex行指向一个新变量p地址Vertex.这不是重新定义了struct的定义吗?设置X,Yint=1,2这里是教程的完整代码:packagemainimport"fmt"typeVertexstruct{X,Yint}var(v1=Vertex{1,2}//hastypeVertexv2=Vertex{X:1}//Y:0isimplicitv3=Vertex{}//

variables - 在 Golang 中附加一个指向 Slice 的指针

https://play.golang.org/p/ghWtxWGOAUfuncTree(Parentnode*Node){ifIsvisitedNode(Parentnode.currentvalue-1){m:=MovesArray[Parentnode.currentvalue-1]forj:=0;j 最佳答案 你有一个错误。在main中,您设置了Y.currentvalue=1。然后在Tree中,currentvalue走到64。X.currentvalue=m[j]fmt.Printf("cv:%v\n",X.curren

variables - if/else 条件定义变量。未定义 : dat (variable)

我知道这是一个基本问题,但我很好奇为什么下面的代码不起作用。没有不声明此变量的用例。if(bundled=="true"){dat,err:=Asset("index.html")}else{dat,err:=ioutil.ReadFile("./index.html")}if(err!=nil){os.Exit(0)}t,_=t.Parse(string(dat))p:=Person{Scope:""}t.Execute(w,p)我得到了错误.\run.go:262:undefined:dat我确定这只是我仍在学习的基本GOLANG知识。感谢您的支持 最佳

sockets - io.复制: How to know if a socket is closed or disconnected

我有一个简单的程序,它将程序的stdin、stdout和stderr连接到一个套接字,就像这样,gofunc(){deferconn.Close();deferstdin.Close();io.Copy(stdin,conn);}();gofunc(){deferconn.Close();deferstdout.Close();deferstderr.Close();io.Copy(conn,stdout);io.Copy(conn,stderr);}();select{}我有两个问题,我必须通过执行select{}让这两个goroutine保持运行当套接字断开连接时,无法通知它。如果

go - 使用 Go 和 Revel,出现错误 c.RenderArgs is Undefined(可能过时了?)

我有一个脚本,其中包含一些带有过时revel的Go代码。我遇到了一个问题:c.RenderArgsundefined(type*revel.ControllerhasnofieldormethodRenderArgs)我试着四处搜索,但无法弄清楚用什么来替换它来修复错误。我不熟悉revel,所以它让事情变得更加困难。如果有人可以提供帮助,或者只是将我链接到一个可以提供帮助的空间(如果我错过了,我很抱歉),我将不胜感激! 最佳答案 使用ViewArgs而不是RenderArgs。 关于go