草庐IT

the_struct

全部标签

struct - 将结构分配给另一个结构

我有一个RegistrationRequest结构:typeRegistrationRequeststruct{Email*stringEmail2*stringUsername*stringPassword*stringNamestring}其中Email2是再次输入的电子邮件值,以验证用户输入的内容是否正确。我还有一个用户结构:typeUserstruct{Email*stringUsername*stringPassword*stringNamestring}当然,除了注册之外,不需要存储Email2。所以我有两个变量:req和u-每个结构一个。是否可以将req结构分配给u结构,

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

Golang 如何将 sizeof struct 提取为 int

我开始学习golang,但遇到了一个我希望是简单的问题。我有一个用C语言编写的文件,其中包含多个结构。即myStruct的现在我想从这个文件中读取一个数据结构。在C中,我简单地打开一个文件并移动结构的fileptr数量*sizeofStruct。像这样intsizeofStruct=sizeof(myStruct)seek(filehandle,searchNo*sizeofStruct)read(filehandle,&data,sizeofStruct)这在Golang中似乎不像“sizeof”那么简单……而是多次转换以uintptr结束……什么的,或者reflect.int32(

go - 将 Go Struct 实例转换为 Lua 表

我有一个GoStruct实例,我想使用GopherLua将该实例传递给Lua方法。我的Go代码是这样的:dog:=new(Animal)runParam:=lua.P{Fn:L.GetGlobal("run"),NRet:1,Protect:true,}mt:=luar.MT(context.AppContext.LuaVM,dog)userData:=&lua.LTable{Metatable:*mt}userData.Append(&lua.LUserData{Value:dog,Metatable:mt,Env:mt.LTable})err=L.CallByParam(runPa

json - 无法将外部结构直接调用到 map[string]struct

我一直在努力找出在解码JSON时为map[string]struct类型调用外部结构的正确方法。当所有代码都在同一个包中时,代码可以正常工作,但是如果它正在提取导出的类型,那么Unmarshal函数似乎有错误。packageanimalstypeBirdstruct{Namestring`json:"name"`Descriptionstring`json:"description"`}packagemainimport("encoding/json""fmt""../animal")funcmain(){birdJson:=`{"birds":{"name":"eagle","des

go - 如何通过struct field Name取值

typemcatstruct{IDint}typecatstruct{NamestringMmcat}funcgetValue(pathstring,mcatcat){//throuthstructpathgetthevalue}funcmain(){mycat:=cat{"cat",mcat{1}}id:=getvalue("/M/ID",mycat)}我可以通过反射获取基于字段名称的值来做到这一点吗? 最佳答案 你可以用Value.FieldByName()做你想做的事功能。只需覆盖可能使用strings.Split()分割的路

go - 当我更新节点或使用 client-go 部署时,我偶尔会遇到这样的错误 "the object has been modified"

当我更新节点或使用client-go部署时,我偶尔会收到这样的错误“对象已被修改;请将您的更改应用到最新版本并重试”。我的目标是添加一个污点/容忍度和一个一个节点/部署的标签。有人说应该用err=retry.RetryOnConflict(retry.DefaultBackoff,func()error{}),但是好像不行。funcAddFaultToleration(deploy*appsv1.Deployment,nsstring,clientkubernetes.Interface)(*appsv1.Deployment,error){updateDeploy,err:=clie

go - Firestore 云函数 : Get DocumentSnapshot from the event

我正在监听收集文档的更改事件,只是转储我收到的内容:funcForwardUserChanged(ctxcontext.Context,ecloudfn.FirestoreEvent)error{raw,err:=json.Marshal(e.Value.Fields)iferr!=nil{returnerr}fmt.Println(string(raw))returnnil}其中FirestoreEvent是自定义结构://FirestoreEventisthepayloadofaFirestoreevent.typeFirestoreEventstruct{OldValueFire

go - Windows API 错误 "The handle is invalid"

我正在尝试创建一个程序,利用MiniDumpWriteDumpWindowsAPI转储另一个进程的内存。但是,它一直返回一个错误,提示Thehandleisinvalid。我对我的进程句柄非常有信心,因为我以前使用过OpenProcessWindowsAPI,所以我认为这就是我使用CreateFileW的方式。我看过网上的例子,比如thisone但我什么也做不了。到目前为止,这是我的代码:packagemainimport("fmt""os""strconv""syscall""unsafe")varkernel32=syscall.NewLazyDLL("kernel32.dll")

json.Marshal(struct) 返回 "{}"

typeTestObjectstruct{kindstring`json:"kind"`idstring`json:"id,omitempty"`namestring`json:"name"`emailstring`json:"email"`}funcTestCreateSingleItemResponse(t*testing.T){testObject:=new(TestObject)testObject.kind="TestObject"testObject.id="f73h5jf8"testObject.name="YuriGagarin"testObject.email="Yu