草庐IT

结构化

全部标签

arrays - 自己结构数组的JSON编码

我尝试读取一个目录并从文件条目中生成一个JSON字符串。但是json.encoder.Encode()函数只返回空对象。为了测试,我在tmp目录中有两个文件:test1.jstest2.jsgo程序是这样的:packagemainimport("encoding/json""fmt""os""path/filepath""time")typeFilestruct{namestringtimeStampint64}funcmain(){files:=make([]File,0,20)filepath.Walk("/home/michael/tmp/",func(pathstring,fo

Go Lang-将结构附加到 slice

是否可以将struct附加到slice?任何人都可以张贴一个例子吗?此slice需要具有struct值。testSlice=make([]Row,10)我试过用这种方式追加,但没有用。testSlice.append(row) 最佳答案 testSlice=append(testSlice,Row{/*...*/}) 关于GoLang-将结构附加到slice,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c

go - go中结构的内存分配

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我最近遇到了http://golang-sizeof.tips/这解释了如何为结构分配内存。我知道为了确保连续的内存分配,我们在为没有填充的变量分配内存时添加填充将不会获得连续的内存。所以我在我的64位计算机上测试了各种组合,发现网站上的结果和我的计算机上的结果不匹配。这是针对这种情况的:typeS2struct{astringbboolebooldint32fboolcstring}主要是,以下

json - 将 "{}"主体解码为结构时,Golang 不会产生错误

在restapi中,当body设置为“{}”时,jsonDecoder不会产生错误。这使得有必要检查目标结构是否仍为nil。我需要检查库是否应该像这样工作,或者这是否是它的问题。//ClientSidethisrequestreq,err:=http.NewRequest("POST","url",strings.NewReader("{}"))//Curlequivalent:curl-XPOST-d'{}'http://api:8080/r/primitives/multiply//ServersidetypeOperandsstruct{Values[]float64`json:

go - 将结构保存到 json 时运行时 : goroutine stack exceeds 1000000000-byte limit,

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion我已经定义了一个gostruct的Trie数据结构。typeNodestruct{ValruneIsWordboolIsRootboolParent*NodeChildrenmap[rune]*Node}typeTriestruct{Root*Node}

go - 为什么在遍历结构片段时无法访问结构字段

我在包中有一个函数,该函数返回我在包中定义的特定结构的slice,然后将其导入到另一个主包中,我想遍历该函数返回的slice并访问structs字段,我收到“未定义”错误。“一个未定义(无法引用未导出的字段或方法a)”我想念什么?谢谢您的帮助。软件包代码:packagetest_packageimport"fmt"typeStructstruct{astringbstring}funcReturnStructSlice()([]Struct){s1:=Struct{"a","b"}s2:=Struct{"c","d"}structSliceToReturn:=[]Struct{s1,s

go - 将结构接口(interface)转换为相同的结构

假设我有两个结构:typePet{Namestring}typePetTwo{Namestring}现在假设我将Pet转换为接口(interface)。假设用于执行此操作的包不知道Pet类型,我如何从界面键入AssertPetTwo?varctxcontext.Contextpet:=Pet{Name:"Foo"}ctx=context.WithValue(ctx,"pet",pet)petTwo:=ctx.Value("pet").(PetTwo)//panicsfmt.Println(petTwo.Name)//prints"Foo"有没有一种方法可以在没有类型断言的情况下访问pe

loops - range 子句针对 map 等不同的数据结构类型返回哪些不同的变量?

当遍历数组时,返回的第一个变量是索引,返回的第二个变量是值:array:=[]int{2,3,4}forindex,value:=rangearray{fmt.Printf("Index:%s,Value:%s\n",index,value)}使用range子句遍历map时返回什么。它与数组不同。无论如何不可能有map的索引。我们能得到键名吗? 最佳答案 根据documentationofrangeclause,以下是与它一起使用的不同类型的返回值:对[n]E、*[n]E或[]E进行数组或slice:第一个值:indexiint第二

go - 我的结构没有正确编码并且缺少一个属性

typeApiResponsestruct{Successbool`json:"success"`Errors[]string`json:"errors"`}typeNewSessionResponsestruct{ApiResponse`json:"apiResponse"`authTokenstring`json:"authToken"`}在我的处理程序中,我这样做:resp:=NewSessionResponse{ApiResponse{true,[]string{}},"auth123"}json.NewEncoder(w).Encode(resp)我看到的响应是这样的:{ap

mysql - 使用 Golang 将 SQL 查询结果放入嵌套结构中

下面是程序的全部代码。它是一种转发请求的服务。正在工作。我想要做的是摆脱当前存储所有配置的yml文件并将它们移动到db。我不想弄乱代码,所以我的想法是将数据库数据简单地存储在相同的结构中。//ConfigcontainsconfigurationforthisservicetypeInstancestruct{Userstring`json:"user"`Passwordstring`json:"password"`InstanceIdstring`json:"instance_id"`InstanceTypestring`json:"instance_type"`InstanceMo