6
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"]
    }
}

I want return map[string]interface{}{} in my code.

but i got compile error syntax error: unexpected [ after top level declaration

if i change map[string]interface{}{} to interface{}, there is no compile error any more.

Attention i use map[string]interface{}{} because i want return a map list.

2
  • for a map list use [] in front. e.g. []map[string]interface{}. Commented Apr 25, 2018 at 4:47
  • @mkopriva if we use []map[string]interface{} it will throw an error. Since ret["result"] is an interface{} Commented Apr 25, 2018 at 4:57

2 Answers 2

8

The code map[string]interface{}{} is a composite literal value for an empty map. Functions are declared with types, not values. It looks like you want to return the slice type []map[string]interface{}. Use the following function:

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

i want return a map list, ret["result"], looks you are return a map, That`s right?
1

Create return type to map[string]interface{}. Because the response returned from GET request is of type map[string]interface{} not map[string]interface{}{} which is a composite literal. So you can use it like

func getLatestTxs() map[string]interface{} {
    fmt.Println("hello")
    // function body code ....
    return ret
}

For this line

if i change map[string]interface{}{} to interface{}, there is no compile error any more.

We can convert anything to interface{} because it works as a wrapper which can wrap any type and will save the underlying type and its value. The value can be received using type assertion for an underlying type. So in your case when you are using an interface it will wrap map[string]interface{} value. For fetching the value it is required to type assert as below.

func getLatestTxs() interface{} {
    fmt.Println("hello")
    // function code .....
    //fmt.Println(ret["result"])
    return ret
}


ret["result"].(interface{}).([]interface{})

print the underlying map[string]interface{} to get the value of single object inside result

fmt.Println(ret["result"].(interface{}).([]interface{})[0].(map[string]interface{})["from"])

1 Comment

fmt.Println(ret["result"].(interface{}).([]interface{})) in your case ret["result"] is actually an interface{} not map[string]interface{} and its innner value is []interface{} slice of interface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.