草庐IT

unsafe-pointers

全部标签

go - 程序在主程序 block 中发现错误后出现 panic 。 panic : runtime error: invalid memory address or nil pointer dereference

我是golang的新手。在定义位置后trycatch主block中的错误后,我的程序出现panic。我在某处读过,添加defer.close()可能会有所帮助,但编译器再次说你的结构中不存在这样的定义帮助我解决它。typeIPInfostruct{IPstringHostnamestringCitystringCountrystringLocstringOrgstringPostalstring}funcmain(){ip:=getLocalIpv4()location,err:=ForeignIP(ip)iferr!=nil{fmt.Println("errorbro")}fmt.P

pointers - 分配嵌入的 Struct 类型值

假设我有以下结构:typeXstruct{NamestringAgeintEmailstring}然后我有2个其他结构(子类):typeAstruct{*X}typeBstruct{*X}然后在我的主要代码中我有:a:=A{&X{"John",34,"jd@email.com"}}b:=B{&X{"Greg",22,"gd@email.com"}}fmt.Println(a.Name)//Johnfmt.Println(b.Name)//Greg是否有一种简单的方法来分配a=b以便a.Name返回Greg以及所有其他属性(我不想单独分配它们,因为有一堆属性)注意:我必须这样做,因为我有

pointers - reflect.Pointer() 返回值不是值地址

typeBookInfostruct{Meta*TableMeta...}func(si*schemaInfo)getTabInfo(objinterface{})(*tabInfo,error){typ:=reflect.TypeOf(obj)val:=reflect.ValueOf(obj)iftyp.Kind()!=reflect.Ptr{returnnil,errors.New("nborm.schemaInfo.getDBInfo()error:requiredapointer")}meta:=*(**TableMeta)(unsafe.Pointer(val.Pointer

go - 使用beego验证码: invalid memory address or nil pointer dereference

我想在Beego下使用captcha生成验证码。但是它有错误无效的内存地址或零指针取消引用。有谁知道如何解决这个问题?谢谢。RequestMethod:GETRequestURL:/accounts/forgotpasswordRemoteAddr:127.0.0.1StackC:/Go/src/runtime/asm_amd64.s:573C:/Go/src/runtime/panic.go:505C:/Go/src/text/template/exec.go:137C:/Go/src/runtime/asm_amd64.s:573C:/Go/src/runtime/panic.go

pointers - 结构体的值接收者

有人可以向我解释为什么r.a的输出为空,因为我已将值2添加到列表中吗?packagemainimport("fmt")funcmain(){varrRr.b=make(map[int]int)r.add()fmt.Println(r)//outputs{[]map[2:2]}}typeRstruct{a[]intbmap[int]int}func(rR)add(){r.a=append(r.a,2)r.b[2]=2} 最佳答案 围棋之旅的一小段摘录指出:Methodswithpointerreceiverscanmodifythev

pointers - 如何以并行方式传递指针的引用?

我正在编写一个机器人来并行运行一些命令并同时并行运行机器人,但我在启动和暂停功能时遇到了问题。下面我将留下一个我设置的例子。预计其中一个bot会继续运行而其他bot会停止,但所有bot最终都会运行。有人可以向我解释为什么在使用startbot()命令时,它没有得到bool值吗?packagemainimport("log""time")typebotBasestruct{isEnabledbool}func(b*botBase)startFunctionX(){b.isEnabled=true}func(b*botBase)pauseFunctionX(){b.isEnabled=fa

go - win.RegisterRawInputDevices 总是返回 false

我正在使用https://github.com/lxn/win在Go中访问低级Windows调用的包。我正在调用win.RegisterRawInputDevices为原始输入数据注册设备,但它始终返回false。我在C#中完成此操作没有任何问题。下面是我的代码:packagemainimport("fmt""syscall""unsafe""github.com/lxn/win")funcWndProc(hWndwin.HWND,msguint32,wParam,lParamuintptr)uintptr{switchmsg{casewin.WM_CREATE:fmt.Println

go - 从字节 slice 到不安全结构的类型转换

我试图理解为什么我在Go中的代码无法按我预期的方式工作。当我执行这个测试时,它失败了:funcTestConversion(t*testing.T){typemyTypestruct{auint8valueuint64}myVar1:=myType{a:1,value:12345}varcopyFrom[]bytecopyFromHeader:=(*reflect.SliceHeader)(unsafe.Pointer(©From))copyFromHeader.Data=uintptr(unsafe.Pointer(&myVar1))copyFromHeader.Cap=9c

pointers - Go指针交换问题

我正在尝试将指针交换为Go中的结构,在附加到它的函数中运行:func(config*Config)ReLoadConfigFile(configPathstring)error{if(len(configPath)>0){newConfig:=Config{};err:=newConfig.LoadFromFile(configPath);//Swaptheconfigs,ensuringtolocktheconfigfortheswapif(err==nil){config.Lock.Lock();deferconfig.Lock.Unlock();config=&newConfig

go - xorm 示例不工作 : "runtime error: invalid memory address or nil pointer dereference"

基于this示例我试图编写一个程序,该程序将从数据库返回一些数据。不幸的是,根据运行时控制台输出,(或多或少)相同的程序结构会在此处导致内存错误err:=orm.Find(&sensorDataEntry)。我在这里错过了什么?示例和我的程序都有使用make()创建的slice,并在Find()方法中使用引用。有问题的代码:packagemainimport("fmt""net/http""time""github.com/gorilla/mux"_"github.com/lib/pq"//"database/sql""github.com/go-xorm/xorm")varorm*x