草庐IT

num_vars

全部标签

go - 如何避免 VSCode 在 Golang 中删除未使用的 var 或导入

这个问题在这里已经有了答案:Howtoavoidannoyingerror"declaredandnotused"(8个答案)HowtodisableGolangunusedimporterror(8个答案)关闭3年前。我正在使用最新的VSCode、golang插件和最新的golang版本学习golang。Goland不会让你有未使用的东西-例如变量和导入。VSCode有办法绕过它吗?谢谢你:)

go - 如何知道在 Golang 中是否填充了一个空的 struct var?

这个问题在这里已经有了答案:Howcheckifapropertywassetinastruct(4个答案)HowtorecognizevoidvalueandunspecifiedfieldwhenunmarshalinginGo?(1个回答)Howtocheckifaspecificpropertyofastructisnull?(1个回答)关闭4年前。在Python中我可以做这样的事情:aModel=Nonemodels=somefunction()formodelinmodels:ifmodel.coolisFalseandmodel.somenumber>-5:aModel=

go - 使用 var vs function 定义函数

导出:varMyFunction=func(){}functionMyFunctionfunc(){}未导出:varmyFunction=func(){}functionmyFunctionfunc(){}我读了varfunctionName=function(){}vsfunctionfunctionName(){}这是关于Javascript的。我考虑从functionmyFunctionfunc(){}更改为varmyFunction=func(){}的原因是后者让我更容易完成我的单元测试。所以我想知道在进行此更改之前是否需要注意什么。 最佳答案

go - 为什么 'var' 不能用在 Go 的 for 循环中?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭3年前。Improvethisquestion我是Go的新手,有一个问题。据我所知,以下两个片段基本相同。i:=0vari=0但是为什么var语句不能用在for循环中呢?forvari=0;i将会出现语法错误:vardeclarationnotallowedinforinitializergo为什么要这样设计?里面有什么思想吗?如果有人能告诉我,我将不胜感激。

go: var declared but not used 错误 - 如何解决?

在这个函数中,我得到了“sdeclaredandnotused”,我不明白-我是否需要以某种方式将其标记为“我真的使用过它”还是什么?funcgetString(datamap[string]interface{},namestring)(string,error){s:=data[name]ifreflect.TypeOf(s).Kind()!=reflect.String{returns.(string),nil}return"",&apiError{1,"it'snotastring"}}奇怪的是,我没有从这个函数中得到错误:funcgetInt(datamap[string]i

go - 如何比较 Int Var 与结构的内容

使用Go1.11。得到以下结构,typeinputsDatastruct{TurnOneintTurnTwointTurnThreeintTurnFourintTurnFiveintTurnSixintTurnSevenintTurnEightintTurnNineint}保存用户输入的数据。没问题,但我想从TurnTwo检查用户输入的内容是否已经输入?示例,输入1->6,输入2->4,输入3->6所以一旦用户再次为第3轮输入6,它会记录,抱歉,再试一次...这就是我检查的方式,funccheckInputTwo(userInputint)bool{varokboolok=trueif

database - 如何从函数返回 ref var

我刚开始学习Go,在从函数返回refvars时遇到问题我有一个从数据库表中获取行的函数:funcgetData(querystring,db*sql.DB)*sql.Rows{rows,err:=db.Query(query)iferr!=nil{fmt.Println("SQLselecterror:")log.Fatal(err)}deferrows.Close()returnrows}现在我尝试从数据库中获取数据rows:=getData("select*fromall_obrash",db)fmt.Println(rows)我希望它只返回*sql.Rows但我一无所获&{0xc

go - 何时使用 var 或 := in Go?

这个问题在这里已经有了答案:WhytherearetwowaysofdeclaringvariablesinGo,what'sthedifferenceandwhichtouse?(1个回答)varvs:=inGo(3个回答)2年前关闭。下面两个例子有什么区别吗?typeExamplestruct{}funcmain(){e:=Example{}}对比typeExamplestruct{}funcmain(){vareExample}有更好的吗?谢谢! 最佳答案 可能值得注意:使用:=当您需要创建一个带有特定值(不是零值)的变量时。

go - 如何在 mux.Vars(request) 中设置一个值

我想在mux.Vars()中设置一个值,MatcherFunc返回true,然后handlerFun可以访问读取。但是当mux.Vars(request)["key"]="value"时,对nil映射中的条目的panic分配如何设置值:domainRouter:=router.MatcherFunc(func(request*http.Request,match*mux.RouteMatch)bool{ifisOk{mux.Vars(request)["key"]="value"returntrue}returnfalse}).Subrouter() 最佳答

go - var _ xerrors.Formatter =包装{}

Thisquestionalreadyhasanswershere:Whatdoesthisgolangcodedo?[duplicate](2个答案)去年关闭。https://github.com/golang/xerrors/blob/master/fmt_test.go#L379var_xerrors.Formatter=wrapped{}var_xerrors.Formatter=detailed{}这两条线的目的是什么? 最佳答案 https://github.com/golang/xerrors该存储库包含新Go1.13