草庐IT

variable_which_I_am_using

全部标签

go - 主.go :9: use of package str without selector

我在TourofGo的解释器中有以下内容:packagemainimport("golang.org/x/tour/wc"str"strings")funcWordCount(sstring)map[string]int{results:=make(map[str]int)words:=str.Fields(s)returnmap[string]int{"x":1}}//funcmain(){//wc.Test(WordCount)//}这是基于https://tour.golang.org/moretypes/23我的错误是tmp/sandbox169629521/main.go:9

unit-testing - httptest.NewRequest 与 http.NewRequest : which one to use in tests and why?

Golang有这两个相似的库http和httptest并且它们都有NewRequest函数。如果http.NewRequest能做到这一切,为什么我们还需要httptest.NewRequest?如果我需要为我的测试创建多部分/多形式请求,我需要使用哪一个? 最佳答案 如文档中所示,httptest.NewRequest“返回一个新的传入服务器请求,适合传递给http.Handler进行测试”,而http.NewRequest“返回适合与Client.Do或Transport.RoundTrip一起使用的请求。”因此,如果您在单元测

go - 我收到错误 fatal error : runtime: out of memory while downloading video using 'go-ipfs-api'

我使用go-ipfs-api从ipfs下载了一个大文件,web访问下载。我收到一个fatalerror:runtime:outofmemory.如何修改我的代码?funcmain(){http.HandleFunc("/",download)http.ListenAndServe(":8080",nil)}funcdownload(whttp.ResponseWriter,r*http.Request){client:=shell.NewShell("http://127.0.0.1:5001")fd,err:=client.Cat("QmTcj7SfRf4vnLnCqnxMT7kut

arrays - 无法对二维数组的列进行 slice "cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment"

我正在使用9x9二维数组的slice制作一个简单的数独游戏。我仍然刚开始使用Golang并且有一些C++经验。我不断收到错误消息“无法将数独[0:9][0](类型[9]int)用作赋值中的类型[]int”。varrow1[]int=数独[0][0:9]该行正确地获取了二维数组第一行的值并将它们放入row1slice中,但是使用varcol1[]int=Sudoku[0:9][0]会导致上面的错误消息。我能做什么?提前致谢!例如,packagemainimport"fmt"funcmain(){varSudoku[9][9]intfmt.Println(Sudoku)varrow1[]i

variables - 短声明运算符隐藏全局变量

有oneFunction返回2个类型的值int和error.我想将第一个值分配给已经存在的变量,并将第二个值分配给一个新变量。如果我使用短声明运算符:=,将创建2个新变量x和err.varxintx,err:=oneFunction()摆脱创建新的x变量我不能使用:=运算符并声明err打电话前oneFunctionvarxintvarerrerrorglob,err=oneFunction()我想知道是否有另一种方法可以将第一个值设置为全局变量而不是创建一个新值? 最佳答案 没有。您声明varerrerror的示例是执行您想要的操作

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

dictionary - 戈朗 : Using a defined interface in a Map Value

在链接的Playground示例中,我定义了一个类型:typeDoMapmap[int]func(Doer)stringDoer在我定义的接口(interface)类型中。我在具体类型MyDoer上实现接口(interface)。我希望能够构造一个DoMap,其中该映射中的条目包含如下两个函数:func(Doer)string//thisworksfunc(*MyDoer)string//thisdoesn'tfunc(MyDoer)string//more-or-lessthesameidea,alsodoesn't我不能,虽然很明显它们是不同的类型,但我想知道为什么我不能,因为函数

go - "Cannot use myType as type interface{}"?我以为在 Go 中所有类型都算作 interface{}?

这个问题在这里已经有了答案:Convert[]stringto[]interface{}[duplicate](3个答案)Convertingsliceofstructstosliceofemptyinterface[duplicate](1个回答)Whycan'tIsubstituteasliceofonetypeforanotherinGo?(3个答案)Whycan'tIpassa`func()[]int`as`func()[]interface{}`ingo?(2个答案)Whyaslice[]structdoesn'tbehavesameas[]builtin?(3个答案)关闭4

variables - 如何通过连接两个字符串来创建动态变量名

我想通过连接当前日期来创建变量名,并为创建的变量名提供一个值。我的变量名应该类似于这样的"Key-2019-01"这样我就可以将值存储为varKey-2019-01="yes"我试过如下。packagemainimport("fmt""time""strconv""strings")funcmain(){currentMonth:=time.Now().Month()currentYear:=time.Now().Year()varmonth=int(currentMonth)varcurrentDate=strings.Join([]string{strconv.Itoa(curre

variables - 如何对变量使用多态性

我是Go新手,开始学习多态性。当多个对象需要使用相同的功能时,我知道如何去做。但是我有一个新问题,如果我在不同的对象上有相同的变量,我不知道该怎么办。在下面的示例中,我有两个不同的对象:struct1和struct2。它们都有相同的变量name。我可以跑过它们并检查哪个是哪个并相应地工作(你可以测试它here):packagemainimport("fmt")typestruct1struct{namestring}typestruct2struct{namestring}funcmain(){structMap:=make(map[string]interface{})s1:=str