使用GO解析API响应

huangapple go评论87阅读模式
英文:

Parsing API response with GO

问题

我正在使用finnhub REST API,但在解析和显示API返回的响应时遇到了问题。

以下是我的代码:

func newTicker(s []string) {
    cfg := finnhub.NewConfiguration()
    cfg.AddDefaultHeader("X-Finnhub-Token", "api_key")
    finnhubClient := finnhub.NewAPIClient(cfg).DefaultApi

    res, _, _ := finnhubClient.SymbolSearch(context.Background()).Q("AAPL").Execute()
    fmt.Printf("%+v\n", res)
    fmt.Printf("%+v\n", reflect.TypeOf(res))
}

以下是输出结果:

Response:  {0xc0003ac168 0xc0003901f8}
TYPE:  finnhub.SymbolLookup

我应该如何查看响应中的数据,并且为什么响应是{0xc0003ac168 0xc0003901f8}

我想要做什么:

只需遍历响应并查看数据即可。

英文:

I'm using the finnhub REST API but I'm having an issue parsing and displaying the response sent back from the API.

Here is my code:

func newTicker(s []string) {
	cfg := finnhub.NewConfiguration()
	cfg.AddDefaultHeader("X-Finnhub-Token", "api_key")
	finnhubClient := finnhub.NewAPIClient(cfg).DefaultApi

	res, _, _ := finnhubClient.SymbolSearch(context.Background()).Q("AAPL").Execute()
	fmt.Printf("%+v\n", res)
	fmt.Printf("%+v\n", reflect.TypeOf(res))
}

Here is the output:

Response:  {0xc0003ac168 0xc0003901f8}
TYPE:  finnhub.SymbolLookup

How do I go about actually viewing the data from the response and why is the response {0xc0003ac168 0xc0003901f8}?

WHAT I'M TRYING TO DO:

Just iterate through the response and view the the data.

答案1

得分: 2

这个响应 {0xc0003ac168 0xc0003901f8} 是指针值的默认文本表示形式。打印出来的十六进制是内存地址。

顺便说一下,格式化动词 %+v 会打印字段名,所以你可能正在显示 %v 的输出结果。

除非结构体实现了 Stringer 接口,否则你不能仅通过 fmt 动词显示指针引用的内容。另一个选项是 %#v,它会添加类型信息,但仅限于此。

你可以定义自己的类型,如下所示:

type StringerSymbolLookup finnhub.SymbolLookup

...并在其上添加一个 String() string 方法来实现 Stringer 接口,并以人类可读的格式打印值。类似于我在 这个 playground 中示例的内容。由于 finnhub.SymbolLookup 只有一个字段,可能不方便转换为字符串,但是两个字段都有 JSON 标签,你可以将其转换为 JSON 并打印出来。

不过,如果你的目标仅是为了开发目的检查响应,使用调试器逐步调试可能是最好的选择。

英文:

This response {0xc0003ac168 0xc0003901f8} is the default textual representation of pointer values. The printed hexes are the memory addresses.

By the way the format verb %+v prints the field names, so you're probably showing the output of %v instead.

Unless the struct implements the Stringer interface, you can't show what the pointers are referencing with just fmt verbs. The other option is %#v which adds type information, but that's about it.

You could define your own type as:

type StringerSymbolLookup finnhub.SymbolLookup

...and add a String() string method on it to implement Stringer and print the values in a human readable format. Something akin to what I exemplified in this playground. Since finnhub.SymbolLookup has one field which is maybe unwieldy to stringify, but has json tags on both fields, you can just marshal to JSON and print that instead.

Though, if your goal is to just inspect the response for development purposes, stepping through it with a debugger may be your best bet.

huangapple
  • 本文由 发表于 2021年9月12日 23:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/69153064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定