Golang返回一个默认的结构体数组。

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

Golang returning a default array of structs

问题

当我使用curl调用API时,我得到的响应如下所示:

[{"Hits":25,"Name":"HIT"},{"Hits":87,"Name":"MISS"},{"Hits":15,"Name":"EXPIRED"}]

当响应返回0值时,即curl返回以下内容:

[{"Hits":0,"Name":"HIT"},{"Hits":0,"Name":"MISS"},{"Hits":0,"Name":"EXPIRED"}]

实际上,在代码中对JSON进行解组后,我得到的是:

[{1 NONE}]

我实际上需要带有0值的键名,因为我最终要将其写入文件,供另一个系统使用。

为了解决这个问题,如果返回值是[{1 NONE}],我创建了一个默认数组。

例如:

type CHData struct {
    Hits int    `json:"Hits"`
    Name string `json:"Name"`
}

func apiResponse() (apiResponseData, error) {

    //调用API并返回响应的代码 {
    ...
    json.Unmarshal([]byte(body), &apiResponseData)
    ..
    }

    if len(apiReturneddata) <= 1 { // 检查是否为[{1 NONE}]值

        //如果API返回0值,则创建默认数组
        array1 := CHData{Hits: 0, Name: "HIT"}
        array2 := CHData{Hits: 0, Name: "MISS"}
        array3 := CHData{Hits: 0, Name: "EXPIRED"}

        //将每个JSON添加到JSON数组中
        var mockData []CHData
        mockData = append(mockData, array1)
        mockData = append(mockData, array2)
        mockData = append(mockData, array3)
        log.Println(mockData)

        i, _ := json.Marshal(mockData)
        //log.Println(string(i))

        //用[{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;HIT&quot;},{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;MISS&quot;},{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;EXPIRED&quot;}]覆盖&quot;[{1 NONE}]&quot;
        json.Unmarshal([]byte(i), &apiResponseData)
    }

    return apiResponseData
}
  1. ^ 这个方法可以工作,但是否有更好的方法来做这个?它似乎很冗长,我只有3个结构体在这个数组中,但我可能会有更多吗?
  2. 为什么它返回[{1 NONE}]而不是键值对结构体?
英文:

When I curl an api I get back a response like this:

[{&quot;Hits&quot;:25,&quot;Name&quot;:&quot;HIT&quot;},{&quot;Hits&quot;:87,&quot;Name&quot;:&quot;MISS&quot;},{&quot;Hits&quot;:15,&quot;Name&quot;:&quot;EXPIRED&quot;}]

When a response comes back with 0 values ie, the curl returns this :

[{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;HIT&quot;},{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;MISS&quot;},{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;EXPIRED&quot;}]

What I actually get after unmarshal the json in code with a 0 value response is:

[{1 NONE}]

I actually need the Key names with the 0 values because I'm writing this out to a file eventually to be consumed by another system.

To get around this I created a default array if the return gives me a [{1 NONE}]

For example:

type CHData struct {
	Hits int    `json:&quot;Hits&quot;`
	Name string `json:&quot;Name&quot;`
}

func apiResponse () apiResponseData,err{

	//code that calls the api and returns the response {
    ...
	json.Unmarshal([]byte(body), &amp;apiResponseData)	
	..
    } 
	

	if len(apiReturneddata) &lt;= 1 { // checks for [{1 NONE}] value
        
		//Default array if api returns with 0 values
		array1 := CHData{Hits: 0, Name: &quot;HIT&quot;}
		array2 := CHData{Hits: 0, Name: &quot;MISS&quot;}
		array3 := CHData{Hits: 0, Name: &quot;EXPIRED&quot;}

		//adding each json into the array of json&#39;s
		var mockData []CHData
		mockData = append(mockData, array1)
		mockData = append(mockData, array2)
		mockData = append(mockData, array3)
		log.Println(mockData)

		i, _ := json.Marshal(mockData)
		//log.Println(string(i))

		//Overwrite the &quot;[{1 NONE}]&quot; with [{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;HIT&quot;},{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;MISS&quot;},{&quot;Hits&quot;:0,&quot;Name&quot;:&quot;EXPIRED&quot;}] 
		json.Unmarshal([]byte(i), &amp;apiResponseData)
    }

	return apiResponseData
}
  1. ^ This works BUT is there a better way to do this it seems very verbose, I only have 3 structs in this array but I could have a lot more?
  2. Why does does it return [{1 NONE}] anyway and not the Key:Value struts?

答案1

得分: 0

我将假设apiResponseData是[]CHData。

你应该检查响应是否为none(1个条目,1个名为None的命中等)。

如果是,请使用默认响应。


type apiResponseData []CHData

var defaultAPIResponseData = apiResponseData{
	{0, "HIT"},
	{0, "MISS"},
	{0, "EXPIRED"},
}

func (r apiResponseData) IsNone() bool {
	return len(r) == 1 && r[0].Name == "None";
}
英文:

I will assume apiResponseData is []CHData

You should check if the response is none ( 1 entry, 1 hit named None etc)

If yes use a default response.


type apiResponseData []CHData

var defaultAPIResponseData = apiResponseData{
	{0, &quot;HIT&quot;},
	{0, &quot;MISS&quot;},
	{0, &quot;EXPIRED&quot;},
}

func (r apiResponseData) IsNone() bool {
	return len(r) == 1 &amp;&amp; r[0].Name == &quot;None&quot;
}

</details>



huangapple
  • 本文由 发表于 2022年3月26日 11:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/71625058.html
匿名

发表评论

匿名网友

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

确定