眭文峰*_*眭文峰 5 interface go go-map
func getLatestTxs() map[string]interface{}{} {
fmt.Println("hello")
resp, err := http.Get("http://api.etherscan.io/api?module=account&action=txlist&address=0x266ac31358d773af8278f625c4d4a35648953341&startblock=0&endblock=99999999&sort=asc&apikey=5UUVIZV5581ENPXKYWAUDGQTHI956A56MU")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("etherscan????")
}
ret := map[string]interface{}{}
json.Unmarshal(body, &ret)
if ret["status"] == 1 {
return ret["result"]
}
}
Run Code Online (Sandbox Code Playgroud)
我想map[string]interface{}{}在我的代码中返回。
但我有编译错误 syntax error: unexpected [ after top level declaration
如果我更改map[string]interface{}{}为interface{},则不再有编译错误。
我使用的注意力map[string]interface{}{}是因为我想返回一个地图列表。
代码map[string]interface{}{}是一个空映射的复合文字值。函数是用类型声明的,而不是值。看起来您想返回切片类型[]map[string]interface{}。使用以下函数:
func getLatestTxs() []map[string]interface{} {
fmt.Println("hello")
resp, err := http.Get("http://api.etherscan.io/api?module=account&action=txlist&address=0x266ac31358d773af8278f625c4d4a35648953341&startblock=0&endblock=99999999&sort=asc&apikey=5UUVIZV5581ENPXKYWAUDGQTHI956A56MU")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("etherscan????")
}
var ret struct {
Status string
Result []map[string]interface{}
}
json.Unmarshal(body, &ret)
if ret.Status == "1" {
return ret.Result
}
return nil
}
Run Code Online (Sandbox Code Playgroud)