I have a struct that looks like
type Request struct {
Name string `json:"name"`
Parameters []Parameter `json:"parameters"`
}
and
type Parameter struct {
Attached bool `json:"attached"`
Script string `json:"script"`
}
Now, I have unmarshalled the json into the struct, and the Script variable has an http location "http://localhost/helloworld.sh". What I am trying to do is, to change the struct variable Parameter.Script
from http://localhost/helloworld.sh
with the actual content of the script, which is a plain ascii shell script. I wrote a method for the inner struct like
func (p *Parameter) SetScript(script string) {
p.Script = script
}
using pointer to Parameter
,
and in the GetScript
function, try to call that method after getting the response body.
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))
}
}
}
However, when I print the struct after calling this function, it has not modified the variables, and prints the http://localhost/helloworld.sh
only.
I am able to get the response body, which is the actual content of the script, but I am not able to replace the struct variable from within the GetScript
function.
Could someone please point out the right way to do this?
Thank you.