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条回答 默认 最新

  • doubu7425 2016-08-02 19:08
    关注

    The function is passing a **interface{} to Unmarshal. To pass the the *[]map[string]interface{} through to Unmarshal, change the function signature to:

     func (self BucketStats) GetAuthRequest(rurl string, data interface{}) (err error) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题