Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将接口中的值转换为golang中的映射?_Go_Response_Go Interface - Fatal编程技术网

如何将接口中的值转换为golang中的映射?

如何将接口中的值转换为golang中的映射?,go,response,go-interface,Go,Response,Go Interface,我完全是个新手 我的目标是获取item.data中的数据,并将它们放到一个映射中,这样我就可以将它们作为键值对进行访问 据我所知,空接口与Typescript中的任何类型都是一样的 你能分享一下在goLang如何做吗 type SearchLogsResponse struct { // The underlying http response RawResponse *http.Response // A list of SearchResponse instanc

我完全是个新手

我的目标是获取item.data中的数据,并将它们放到一个映射中,这样我就可以将它们作为键值对进行访问

据我所知,空接口与Typescript中的任何类型都是一样的

你能分享一下在goLang如何做吗

type SearchLogsResponse struct {

    // The underlying http response
    RawResponse *http.Response

    // A list of SearchResponse instances
    SearchResponse `presentIn:"body"`

    // For list pagination. When this header appears in the response, additional pages
    // of results remain. For important details about how pagination works, see
    // List Pagination (https://docs.cloud.oracle.com/iaas/Content/API/Concepts/usingapi.htm#nine).
    OpcNextPage *string `presentIn:"header" name:"opc-next-page"`

    // Unique Oracle-assigned identifier for the request. If you need to contact
    // Oracle about a particular request, please provide the request ID.
    OpcRequestId *string `presentIn:"header" name:"opc-request-id"`
}
//SearchResult日志搜索结果条目

type SearchResult struct {

    // JSON blob containing the search entry with projected fields.
    Data *interface{} `mandatory:"true" json:"data"`
}


// SearchResponse Search response object.
type SearchResponse struct {
    Summary *SearchResultSummary `mandatory:"true" json:"summary"`

    // List of search results
    Results []SearchResult `mandatory:"false" json:"results"`

    // List of log field schema information.
    Fields []FieldInfo `mandatory:"false" json:"fields"`
}

res, err1 := o.loggingSearchClient.SearchLogs(ctx, request)
    for _, item := range res.Results {

            //TODO create a map string [string]

            // Put key value pairs from item into the above map




}
备注:项中的数据。数据是一个键值对列表,其中每个键可以包含另一个值为字符串的接口中的数据

编辑:关键点是动态的


首先:不要使用
*接口{}
,而是使用
接口{}

您可以使用类型断言测试
数据的底层类型
,如果它是
map[string]接口{}
,则递归下降:

(您提到这是SDK的一部分,无法更改,因此解决方法是取消对接口的引用)


你知道intem中的所有键。数据或键列表是动态的吗?键是动态的。同时更新我的问题并显示代码。什么是商品?什么是数据?@BurakSerdar DoneSorry,没有人会检查600行的源文件。将相关部分添加到您的帖子中。首先:不要使用*interface{},而是使用interface{}。这是无法做到的,因为它是正在导入的sdk的一部分。您可以建议解决方法吗?编辑了解引用的答案
if m, ok:=(*item.Data).(map[string]interface{}); ok {
   // m is a map[string]interface{}
   etag:=m["data.eTag"]
   // etag is an interface{}
   if str, ok:=etag.(string); ok {
      // str contains the string value
   }
}