草庐IT

use-mirrors

全部标签

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

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

戈朗 : How do you use a pointer on a struct that hasn't been initialized yet

所以我在看filehere.他们调用record:=&accessLog但他们从来没有首先将其初始化为变量,如果他们这样做,如果有多个同时连接,记录是否有可能被覆盖用别人的数据?typeaccessLogstruct{ip,method,uri,protocol,hoststringelapsedTimetime.Duration}funcLogAccess(whttp.ResponseWriter,req*http.Request,durationtime.Duration){clientIP:=req.RemoteAddrifcolon:=strings.LastIndex(cli

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

go - 仅在其他包中使用变量时如何避免 "declared and not used"?

我看到了这篇文章Howtoavoidannoyingerror"declaredandnotused"但我不知道这是不是处理错误的正确方法,当我在其他包中使用该变量时。例如,如果我只在其他包中使用Connect(),那么我不会在这个包中使用变量db。funcConnect(){db,err:=sql.Open("mysql","root:Berlin2018@/jplatform")iferr!=nil{panic(err.Error())}} 最佳答案 避免声明和未使用的“烦人”的最佳方法是您不应该声明您未使用的变量,例如,如果您

arrays - 戈朗 : using nested structs

我有两个结构:typepersonstruct{namestringageint}typeclassstruct{students[]person}假设在main函数中,我创建并填充了两个person变量,然后我想将它们添加到具有类类型的变量中。我该怎么做?即s:=person{name:"Sean",age:50}t:=person{name:"Nicola",age:35}我如何将s和t放入:lab:=class? 最佳答案 下面应该实现你想要的:lab:=class{[]person{s,t}}测试一下here.在您继续您的项

go - 在 Golang 中使用全局列表变量。接收 "Use of package list without selector"

我有一个名为rooms的全局链表。它将存储该用户输入的所有房间的名称。在我的函数创建中,我试图引用这个名为房间的列表。我在我的主要功能中实例化列表。当我尝试将项目添加到列表“房间”时,我收到错误“使用不带选择器的包列表”。我希望能够从我的创建函数中向我的名为房间的列表中添加一个字符串。packagemainimport("net""fmt""bufio""os""container/list")varroomslistfunccreate()string{reader:=bufio.NewReader(os.Stdin)fmt.Print("NametheChatroom");inpu

go - 未能使测试套件通过 golang "sayHello() used as value"

我试图让这个测试套件通过命令提示符(hello-world_test.go):packagehelloworldimport"testing"funcTestHelloWorld(t*testing.T){output:=sayHello()if"Hello,World!"!=output{t.Fatalf("output:%s\nexpected:Hello,World!",output)}}我的代码如下(helloworld.go):packagehelloworldimport"fmt"funcsayHello(){fmt.Println("Hello,World!")}通过命令