草庐IT

api接口

全部标签

struct - 在将它作为接口(interface)传递并运行它的方法后,让你将结构恢复为它的数据类型?

我有一个结构如下typeMyStruct{EmbeddedFooBar}func(m*MyStruct)Foo(b*http.Request){//Doingsomething}funcfn(args...interfaces){//It'shereIwanttogetmystructbackandrunthe"Get"method//PleasekeepinmindIamtoopassapointerparamintothestructmethodstrt:=args[0]....getstructbacktostaticdatatypeMyStructandrun"Get()",d

pointers - 在 golang 中打印一组空接口(interface)?

我有一个结构,其字段形式为field[]interface{}。如果我打印该字段,我会得到一个指针引用。如果我尝试取消引用该字段,我会收到“无效间接”错误。代码如下:typeMyTypestruct{field[]interface{}}myType:=//createaMyType.Fieldisjustanarrayofnumbersprintln(myType.field)//printsapointerreference,ex:[1/1]0xc420269aa0println(*(myType.field))//doesn'tcompile如何打印myType.field中的值

json - 如何解析由 cryptocompare API 生成的 JSON?

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭5年前。Improvethisquestion我正在尝试使用此API端点解析一些JSON。https://www.cryptocompare.com/api/data/coinlist/我可以看到它使请求正常,然后我尝试解码响应的主体,它返回大量随机数。如果我从调试器复制正文值,我会得到以下结果。(

postgresql - 通用 REST API Golang

在SO中搜索GenericRESTAPIGolang得到0个结果。搜索谷歌给出2个结果。所以这个问题可能没有正确表述或者不可能在Golang中实现。我的目标是避免一遍又一遍地重复类似的代码。所以我试图让Golang中的代码尽可能通用。一次编写,多次使用。这是我第一次尝试在Golang中为select创建一个通用的RESTAPI。下面的代码几乎给出了我想要的:但结果显示在终端中。我不知道如何将结果重定向到浏览器。packagemainimport("fmt""log""net/http""database/sql""time"_"github.com/lib/pq")vardb*sql.

json - 在 GO lang 中使用 API 时如何将 JSON 转换为 Go 类型定义

我正在构建一个使用API的应用程序,然后还将json数据保存到golang结构中,稍后我将制作端点,为某些计算提供结果。我已经实现了使用API,具有挑战性的部分是如何以go理解的方式保存数据。哪种方法合适?下面是我发起请求时的JSON格式。我感兴趣的key只有TimeSeries(1min)JSON{"MetaData":{"1.Information":"Intraday(1min)pricesandvolumes","2.Symbol":"MSFT","3.LastRefreshed":"2018-05-2416:00:00","4.Interval":"1min","5.Outp

go - 在 Go 中使用外部 API 后使用 REST 客户端保存数据

我是Go的新手lang创建了一个使用alphavantage的REST客户端接口(interface)在我发出GET请求后出现的JSON结构如下所示。我只需要TimeSeries关键数据,以便我自己进行计算。如何从TimeSeries获取数据并保存它以便我自己对数据进行操作?JSON{"MetaData":{"1.Information":"Intraday(1min)pricesandvolumes","2.Symbol":"MSFT","3.LastRefreshed":"2018-05-2516:00:00","4.Interval":"1min","5.OutputSize":

json - 将接口(interface)传递给函数

我正在尝试编写一个采用json文件名和配置结构的配置包。它应该将json解码到传入的结构中并返回它。我正在尝试使用接口(interface),以便我可以传递任何我想要的结构错误是:panic:接口(interface)转换:interface{}是map[string]interface{},不是*main.ConfigurationData我不太确定如何解决这个问题。这是我的主包packagemainimport("config""commons")typeConfigurationDatastruct{S3ARNstring`json:"S3ARN"`SQSQueueUrlstri

go - 创建一个函数来测试从接口(interface)编码/解码

我想创建一个简单的函数来测试编码/解码记录是否按预期工作。我只是在这个例子中使用JSON:packagetestimport("encoding/json""fmt""testing""reflect""github.com/stretchr/testify/require")funcCheckRoundTripJSON(t*testing.T,recordinterface{}){data,err:=json.Marshal(record)require.NoError(t,err)fmt.Println("Record:",record,"wasencodedto:",data,"

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

slice 接口(interface)上的 Golang Switch Case

是否可以区分switchcase中的[]interface{}和interface{}?尝试创建一个解码函数,您可以在其中传递不同的类型,然后switchcase确定类型,然后继续解码该特定类型。虽然当传递的类型是[]interface{}时我遇到了问题。我一直在试验reflect包,但到目前为止运气不好。请参阅下面的代码片段和Playground链接。packagemainimport("fmt""math/big")typeTeststruct{tinterface{}}funcmain(){testVar1:=big.NewInt(0)testVar2:=int64(1)test