草庐IT

pointer_traits

全部标签

pointers - 使用 xlsx 包 panic : runtime error: invalid memory address or nil pointer dereference Go

var(file*xlsx.Filesheet*xlsx.Sheetrow*xlsx.Rowcell*xlsx.Cell)funcaddValue(valstring){cell=row.AddCell()cell.Value=val}并从http://github.com/tealeg/xlsx导入当控制权到达这条线时cell=row.AddCell()这是panic。错误:panic:runtimeerror:invalidmemoryaddressornilpointerdereference有人可以建议这里出了什么问题吗? 最佳答案

go - 无法调用 unsafe.Pointer(指向 C 函数)作为函数

我正在尝试调用具有相同签名的C函数,它们采用2个int参数。并且这个错误cannotcallnon-functionf(typeunsafe.Pointer)在编译时出现。packagemain/*intadd(inta,intb){returna+b;}intsub(inta,intb){returna-b;}*/import"C"import("fmt""unsafe")funcmain(){a:=C.int(1)b:=C.int(2)fx:=make([]unsafe.Pointer,2)fx[0]=C.addfx[1]=C.subfor_,f:=rangefx{fmt.Prin

pointers - Go函数指针问题

这个问题在这里已经有了答案:MyobjectisnotupdatedevenifIusethepointertoatypetoupdateit(3个答案)GolangOperatorOverloading(1个回答)Golangchangingvaluesofastructinsideamethodofanotherstruct(2个答案)CopyinstancesoftypeT,whenanyofthemethodsofanamedtypeThaveapointerreceiver(1个回答)关闭5年前。我有一个结构typekeeperstruct{ptrint32}然后我给它添加一

pointers - Go 中的指针解除引用是如何工作的?

我正在http://tour.golang.org/学习golang教程,并在example29中尝试了一些东西为了方便大家引用,原例子复制在这里:packagemainimport"fmt"typeVertexstruct{X,Yint}var(p=Vertex{1,2}//hastypeVertexq=&Vertex{1,2}//hastype*Vertexr=Vertex{X:1}//Y:0isimplicits=Vertex{}//X:0andY:0)funcmain(){fmt.Println(p,q,r,s)}它非常基础,展示了如何创建这个奇特的新结构Vertex的实例。E

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: