使用指针在go

时间:2016-06-15 12:55:18

标签: json pointers methods go struct

我的结构看起来像

type Request struct {
    Name string `json:"name"`
    Parameters []Parameter `json:"parameters"`
}

type Parameter struct {
    Attached bool `json:"attached"`
    Script string `json:"script"`
}

现在,我已经将json解组到struct中,而Script变量有一个http位置" http://localhost/helloworld.sh"。我想要做的是,将Parameter.Script的结构变量http://localhost/helloworld.sh改为脚本的实际内容,这是一个简单的ascii shell脚本。我为内部结构编写了一个方法,如

func (p *Parameter) SetScript(script string)  {
    p.Script = script
}

使用指针Parameter

并在GetScript函数中,尝试在获取响应主体后调用该方法。

func GetScript(params *Request)  {
  for _, i := range params.Parameters {
    switch i.Attached {
    case false:
        client := new(http.Client)
        req, _ := http.NewRequest("GET", i.Script, nil)
        resp, _ := client.Do(req)
        defer resp.Body.Close()
        reader, _ := ioutil.ReadAll(resp.Body)
        i.SetScript(string(reader))
    }
  }
}

但是,当我在调用此函数后打印结构时,它没有修改变量,只打印http://localhost/helloworld.sh。 我能够获得响应主体,这是脚本的实际内容,但我无法从GetScript函数中替换结构变量。 有人可以指出正确的方法吗?

谢谢。

1 个答案:

答案 0 :(得分:4)

问题是你正在使用for _, i := range循环,并修改循环内的循环变量:

for _, i := range params.Parameters {
    switch i.Attached {
    case false:
        // ...
        i.SetScript(string(reader))
    }
}

循环变量i是您定位的切片元素的副本。因此,如果对其进行任何修改,则只会修改副本而不是修改切片中的元素。 (请注意,SetScript()方法确实有一个指针接收器,但它会收到副本的地址,因此它可以并且只会修改副本。)

一种解决方法是使用仅索引range,并使用索引引用切片元素(将所有i替换为params.Parameters[i]):

for i := range params.Parameters {
    switch params.Parameters[i].Attached {
    case false:
        // ...
        params.Parameters[i].SetScript(string(reader))
    }
}

您可以通过将切片分配给局部变量来简化上面的代码(这将仅复制切片头而不是其元素,并且它将引用相同的基础数组),并使用if语句而不是丑陋的switch

p := params.Parameters
for i := range p {
    if !p[i].Attached {
        // ...
        p[i].SetScript(string(reader))
    }
}

另一种简化/改进是获取索引表达式的地址,并使用它(因此您可以省略多次重复):

for i := range params.Parameters {
    p := &params.Parameters[i]
    if !p.Attached {
        // ...
        p.SetScript(string(reader))
    }
}