我是 GO 的新手。我的目標是獲取 item.Data 中的數(shù)據(jù)并將它們放入地圖中,以便我可以將它們作為鍵值對進行訪問。據(jù)我了解,空接口與 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}PS:item.Data 里面的數(shù)據(jù)是一個鍵值對的列表,其中每個鍵可以包含另一個接口內(nèi)的數(shù)據(jù),其值是一個字符串
1 回答

動漫人物
TA貢獻1815條經(jīng)驗 獲得超10個贊
第一:不要使用*interface{},而是使用interface{}。
您可以使用類型斷言來測試底層類型,Data如果它是 a 則遞歸下降map[string]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
}
}
- 1 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消