草庐IT

stack-pointer

全部标签

pointers - 指向方法参数中接口(interface)的指针?

Gistwithcode如何在第30行使用接口(interface)Herbivore代替*Mouse?我想将实现Herbivore接口(interface)的不同结构传递给方法eatingVictim,而不仅仅是Mouse 最佳答案 让我解释一下:首先在这个函数中:func(predatorCat)eatingVictim(victim*Mouse){fmt.Println(predator.name+"'seatingvictim"+victim.name)predator.hungry=falsevictim.alive=fa

pointers - 隐藏 nil 值,理解为什么 golang 在这里失败

我不明白如何正确确保某些东西不是nil在这种情况下:packagemaintypeshowerinterface{getWater()[]shower}typedisplaystruct{SubDisplay*display}func(ddisplay)getWater()[]shower{return[]shower{display{},d.SubDisplay}}funcmain(){//SubDisplaywillbeinitializedwithnulls:=display{}//water:=[]shower{nil}water:=s.getWater()for_,x:=ra

pointers - 在 Go 中使用类似的指针结构分配结构

我有两个相似的结构,我想将一个分配给另一个。第一个“Equipment”是用来匹配数据库的结构。第二个“JsonEquipment”是解析JSON数据的辅助结构。例子如下:typeEquipmentstruct{IDuintCategoryIDuintIpstringLoginstringPasswordstring}typeJsonEquipmentstruct{ID*uintCategory*stringIp*stringLogin*stringPassword*string}指针用于检查该字段是否存在于JSON中。更多信息:Howtorecognizevoidvalueandun

pointers - 为什么带有指针接收器的方法在接收到值时仍然有效?

我只是在玩Exercise51intheTourofGo.该解释声称Scale方法在接收到Vertex而不是指向Vertex的指针时无效。然而,当我在main中将声明v:=&Vertex{3,4}更改为v:=Vertex{3,4}>输出中唯一的变化是缺少标记指针的&。那么为什么Scale会更改它接收到的变量,即使该变量不是指针? 最佳答案 它不“接收”一个值。Go是强类型的,因此如果在某处规定了指向T的指针,则指向T(*T)的指针是唯一可以作为此类类型位置的值发生的选项。“魔法”在编译器中,它在某些conditions下有效地“重写

pointers - 在 Golang 中取消引用 map 索引

我目前正在学习Go,我制作了这个简单粗暴的list程序,只是为了修补结构和方法以了解它们的工作原理。在驱动程序文件中,我尝试从Cashier类型的项目映射中调用方法和项目类型。我的方法有指针接收器直接使用结构而不是制作副本。当我运行程序时出现此错误.\driver.go:11:cannotcallpointermethodonf[0].\driver.go:11:无法获取f[0]的地址Inventory.go:packageinventorytypeitemstruct{itemNamestringamountint}typeCashierstruct{itemsmap[int]ite

pointers - []struct{} 和 []*struct{} 有什么区别?

下面有什么区别?typeDemostruct{sstring}funcgetDemo1()([]*Demo)//1funcgetDemo2()([]Demo)//2getDemo1和getDemo2在内存上有区别吗? 最佳答案 我要回答这个问题,尽管我的判断更好,只是将OP发送到导览和文档/规范。主要是因为:IsthereanymemorydifferencebetweengetDemo1andgetDemo2?这个具体问题的答案取决于您如何使用slice。Go是按值传递,因此传递结构值会复制它们。例如,请考虑以下示例。https:

pointers - 为什么接口(interface)类型不提供 "IsNil"方法?

首先让我们考虑以下几点:funcprocess(bodyio.Reader){fmt.Printf("body==nil?%+v\n",body==nil)}funcmain(){varbody*bytes.Bufferfmt.Printf("body==nil?%+v\n",body==nil)process(body)process(nil)}这是输出:body==nil?truebody==nil?false//Didyougetthisright?body==nil?true另一个例子:typeContainerstruct{Readerio.Reader}funcproces

pointers - 空结构 slice 的地址

我有一个关于emptystruct的基本问题,我试图了解在尝试获取两个slice的后备数组元素的地址时得到的以下不同输出:a:=make([]struct{},10)b:=make([]struct{},20)fmt.Println("&a==&b",&a==&b)fmt.Println("&a[0]==&b[0]",&a[0]==&b[0])上面的片段returns:&a==&bfalse&a[0]==&b[0]true但是,考虑以下略有更改的代码段:a:=make([]struct{},10)b:=make([]struct{},20)fmt.Println(a[0],&a[0])

Go Error : panic: runtime error: invalid memory address or nil pointer dereference. Changing map inside a struct which is present in 另一个结构,

这个问题在这里已经有了答案:map[string]*type"invalidmemoryaddressornilpointerdereference"(1个回答)关闭3个月前。我必须结构让我们说struct1和struct2,struct2包含一个带有struct1的映射,struct1也包含一个映射,我想更改struct1中存在的映射。这是抛出一个运行时错误:panic:运行时错误:无效内存地址或零指针解引用typeFailureDatastruct{failuresInCommitsmap[string][]string}typeDetectionResultsstruct{Fai

pointers - 如何在 Go 中存储对操作结果的引用?

好吧,很难用语言来描述它,但假设我有一个存储int指针的映射,并且想将操作的结果存储为我的散列中的另一个键:m:=make(map[string]*int)m["d"]=&(*m["x"]+*m["y"])这不起作用并给我错误:cannottaketheaddressof*m["x"]&*m["y"]想法? 最佳答案 指针是内存地址。例如,一个变量在内存中有一个地址。像3+4这样的操作的结果没有地址,因为没有为它分配特定的内存。结果可能只存在于处理器寄存器中。您必须分配可以将其地址放入map的内存。最简单直接的方法是为它创建一个局部