douwuli4512
2016-08-02 19:04
浏览 39
已采纳

如何在Golang中通过引用传递带有值类型接口的地图切片

I want to pass []map[string]interface{} by reference to a function. Here is my attempt.

package main

import "fmt"

func main() {

    b := map[string]int{"foo": 1, "bar": 2}
    a := [...]map[string]int{b}

    fmt.Println(a)

    editit(a)

    fmt.Println(a)
}

func editit(a interface{}) {
    fmt.Println(a)

    b := map[string]int{"foo": 3, "bar": 4}
    a = [...]map[string]int{b}

    fmt.Println(a)
}

https://play.golang.org/p/9Bt15mvud1

Here is another attempt and what I want to do finally. This does not compile.

func (self BucketStats) GetSamples() {

    buckets := []make(map[string]interface{})
    self.GetAuthRequest(self.url, &buckets)

    //ProcessBuckets()
}

func (self BucketStats) GetAuthRequest(rurl string, **data interface{}) (err error) {
    client := &http.Client{}

    req, err := http.NewRequest("GET", rurl, nil)
    req.SetBasicAuth(self.un, self.pw)
    resp, err := client.Do(req)
    if err != nil {
            return
    }

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
            return
    }

    // it's all for this!!!
    err = json.Unmarshal(body, data)

    return
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题