草庐IT

C_Func_using_Func_ptr

全部标签

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 - go1.10之前的Ceiling Func

我需要一个自定义的“Ceil”函数,它像在go1.10中一样工作,就像我们在v1.9上一样(obv不会那么高效,但没关系)例如天花板(0.33)=1.00我已经看到了一些通用的最近整数舍入解决方案,但是,想知道是否有人已经为v1.9实现了equiv'Ceil'func作为变通方法? 最佳答案 由于Go是开源的,你可以直接使用他们的代码:https://golang.org/src/math/floor.go?s=720:748#L26我已经查看了代码并将所有的点点滴滴提取到thislittleprogram中给你:packagema

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 - 在内存地址调用 func

我有一个映射,其中一个结构作为键,一个函数作为值,我想在检索给定键的值时调用函数map[struct]funcmap[{contact%!s(int=1)}:%!s(main.Controller=0x4c7b50){services/basket%!s(int=2)}:%!s(main.Controller=0x4c7ad0){categories%!s(int=1)}:%!s(main.Controller=0x4c7ae0){categories/{category}%!s(int=2)}:%!s(main.Controller=0x4c7af0){categories/{cat

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 - 不能使用函数(类型 func())作为参数类型

packagemainimport("log""strings""asl.com/asl")/*TrivialservicetodemonstratechainingservicetogetherMessagestartsinoriginator,travelsthroughacoupleformatters,andthengetsbacktooriginator*/typeMessageTeststruct{Bodystring`json:"body"`}vars*asl.Servicefuncmain(){var(errerrorcidstring)//varmMessageDel

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

go - 不能在 func 参数中使用 bytes.Buffer 作为 io.WriterAt 类型

今天在go上苦苦挣扎..我不得不问的第二个问题。我有2个测试写入函数Write(),它采用writerio.WriterAt和contentinterface{}.我正在处理为函数编写的(2)个测试,TestWriteSuccessful和TestWriteFail。我在测试这两个函数时得到的错误是:cannotuse&b(type*bytes.Buffer)astypeio.WriterAtinargumenttoWrite:问题什么实现了我可以在这些测试中替换bytes.Buffer以使测试正常运行的WriterAt?我尝试过的将b的类型更改为os.File-b.len()>0将失

戈朗 : 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

algorithm - 不能在 time.AfterFunc 的参数中使用(属于)类型 func() 的函数

如果不遇到几个嵌套函数问题,我不知道如何解决这个Go算法问题。其中之一是,“不能在返回参数中使用func文字(类型func())作为类型func()字符串”。我现在使用的解决方案是://Writeafunctionthattakesin2numbers(a,b)andafunction.//Itshouldexecutethefunctionafteramilliseconds,//andthenexecutethefunctionagainafterbmilliseconds.packagemainimport"time"funcnewFunc(bint,fnfunc()string