草庐IT

类结构

全部标签

go - 如何知道结构或结构指针是否实现接口(interface)

我需要知道结构或指向该结构的指针是否实现了给定的接口(interface)。//Youcaneditthiscode!//Clickhereandstarttyping.packagemainimport"fmt"funcmain(){varaA=A{i:5,}Serialize(a)Serialize(&a)}typeSerializableinterface{//Serialize()string//Deserialize(string)Serializebyte()[]byteDeserializebyte(b[]byte)(bytesReadint)}typeAstruct{i

json - 将结构编码为 json Go

我在将结构编码为json时遇到问题,我的代码是typeMainStructurestruct{Textstringjson:"text,omitempty"Array[]TestArrayjson:"test_array,omitmepty"}typeTestArraystruct{ArrayTextstringjson:"array_text,omitempty"}funcmain(){Test:=MainStructure{Text:"test",Array:[{ArrayText:"test1"},{ArrayText:"test2"}]}body:=new(bytes.Buff

go - 如何将一段结构编码为有效的 JSON

我正在编写Golangapi和客户端,但无法从api中的结构片段中获取有效的json。我在客户端中得到的结果如下所示。[{0Mark1234false}{0John3456false}]我需要这个json看起来像[{"id":0,"name":Mark,"pin":1234,"active":false}{"id":0,"name":John,"pin":3456,"active":false}]我找不到向我展示如何正确编码的示例,尽管有警告,这不是我能找到的任何内容的重复。虽然我的客户端成功地将JSON解析回结构,但我还需要它将JSON返回给请求它的IOS客户端。流程是API->AP

go - Golang 结构体名称和类型后面的字符串是什么?

这个问题在这里已经有了答案:WhatisthethirdparameterofaGostructfield?(2个答案)Whatistheusageofbacktickingolangstructsdefinition?[duplicate](2个答案)StrangetypedefinitionsyntaxinGolang(name,thentype,thenstringliteral)(1个回答)GoStringaftervariabledeclaration(2个答案)StringliteralsinGOstructuredefinition[duplicate](1个回答)关闭3

go - 如何在 Golang 中将不同类型的结构作为参数传递给函数

我有一个将http响应json解码为结构的函数。我有两种类型的结构需要传递给此函数,并将结构类型作为返回值以获取解码的json。我的函数现在可以处理一种类型,需要帮助才能处理不同类型的结构,并返回该结构。//ResponsejsontyperesponseResultstruct{resultstring}typeloginResultstruct{responseResulttokenstring}funcresponseBodyDecoder(resphttp.Response,response*responseResult){//getresultformResponsedeco

Go Lang 中的 JSON 结构到 csv

寻找在保留层次结构的同时将JSON读取结构导出为某种csv格式的想法。https://play.golang.org/p/jf2DRL1hC5K/*Expectedoutputinexcelfordatawrangling:AKey|BKey|CKey|DKeySomethingASomethingBSomethingCSomethingFSomethingASomethingBSomethingCSomethingGSomethingASomethingBSomethingC[1,2,3]*/我试过如下遍历结构for_,value:=rangemymodel{fmt.Println(

go - 重用结构以减少堆使用

请解释以下代码中发生的事情。我不明白的部分是服务结构。服务结构是APIClient结构的包装器。当NewAPIClient被调用时,它使用内部的服务结构并复制到自身。我似乎无法解决这个问题。请指教并详细说明。谢谢你。typeAPIClientstruct{cfg*Configuration//Reuseasinglestructinsteadof//allocatingoneforeachserviceontheheap.commonservice//APIServicesAccountApi*AccountApiServiceContractApi*ContractApiServic

go - 在 Golang 中使用属于另一个包的结构

在我的主包中,我有:typeInfoToSendstruct{idstringfield1stringfield2string}然后我调用方法发送:err=rpc.SendValue(toSend.id,toSend.field1,toSend.field2)我想将其重构为:err=rpc.SendValue(toSend)但在rpc包中,我无法访问main.InfoToSend结构。funcSendValue(infoInfoToSend)error{...}我们能做些什么? 最佳答案 让我们从逻辑上看一下。这属于什么域:type

go - 如果接口(interface)类型的变量是实现它的某种结构类型,如何检查 Go?

假设我有以下内容:typeAInterface{....}//ImplementsAtypeBstruct{....}//ImlementsAtypeCstruct{....}现在我有一个函数,它接受A类型的变量作为参数:funcFoo(objA){ifAisB{....}elseifAisC{....}}还有一个main函数:funcmain(){b:=B{}Foo(b)}如何检查传递给函数的参数是否实际上是B类型? 最佳答案 使用typeswitch如前所述,@CeriseLimón链接的旅游页面。funcFoo(vA){swi

google-app-engine - 如何将结构数据放入go-lang的数据存储中

很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭9年前。如何在go-lang中将结构数据放入数据存储区://结构已创建。typeUserLoginstruct{userNamestringpassWordstring}//valuesassignedp1:=UserLogin{"poonam","mumbai123"}p2:=UserLogin{密码:"mumbai321",用户名:"abcd"}现在如何存储/放置/保存以上p1、p2到datastor