草庐IT

any_variable

全部标签

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

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知识。感谢您的支持 最佳

html - 在 PHTML 文件中使用 Go .Variable inside range block

我有一个.thtml文件:...{{.Something}}{{range...}}{{.Something}}{{end}}...如果我在.thtml文件中使用.Something的值,它工作正常,但如果在{{range中以相同的方式使用它,它就不起作用。..}}block。我该如何使用它? 最佳答案 游标被{{range}}修改。将光标分配给一个变量并在范围内使用该变量。...{{.Something}}{{$x:=.}}{{range...}}{{$x.Something}}{{end}}...playgroundexampl

variables - 无法访问函数内部的全局变量

这个问题在这里已经有了答案:Howtouseglobalvaracrossfilesinapackage?(3个答案)关闭4年前。我有以下代码:packagemainimport("net/http""log""net""fmt""os""encoding/json")constconfigNamestring="config.json"typeConfigstruct{UDPServerAddressstringHTTPServerAddressstring}varconfigConfigfuncUDProutine(querystring,chchan在我的config.json{

go - 可变 slice 作为参数错误 :cannot initialize 2 variables with 1 value

尝试使用可变参数组合多个slice,我收到错误:无法用1个值初始化2个变量如何调用这个Combine函数?代码如下:funcCombine(ss...[]string)[]string{mp:=map[string]bool{}for_,s:=rangess{for_,v:=ranges{ifv!=""{if_,ok:=mp[v];!ok{mp[v]=true}}}}combined:=[]string{}forv:=rangemp{combined=append(combined,v)}returncombined}tests:=[]struct{caseNamestrings1[]

go - undefined variable golang

这个问题在这里已经有了答案:Variablescopeinsideifstatements(2个答案)关闭4年前。有人能告诉我为什么num是未定义的吗::这是goplayground链接,你也可以在这里查看这段代码:https://play.golang.org/p/zR9tuVTJmx-packagemainimport"fmt"funcmain(){if7%2==0{num:="first"}else{num:="second"}fmt.Println(num)}

variables - 我是 Golang 的新手,希望解释以下作业

我是Golang的新手,希望有人向我解释以下代码,尤其是分配Pos(0)时的最后一部分。Pos(0)到底是什么?谢谢!typePosuintvarNoPos=Pos(0) 最佳答案 这是一个typeconversion.它可以将0转换为类型Pos。它也可以在没有像这样的转换的情况下重写:varNoPosPos=0 关于variables-我是Golang的新手,希望解释以下作业,我们在StackOverflow上找到一个类似的问题: https://stack