Issue
i’ve struct with value and I run with loop to update value, while debug the code
I see that it enter to the case lines like element.Type = "cccc”
but after the loop exit when I look
at ftr the old value exist and its not updated, what am I missing here ?
ftr := FTR{}
err = yaml.Unmarshal([]byte(yamlFile), &ftr)
for index, element := range ftr.Mod{
switch element.Type {
case “aaa”, “bbbb”:
element.Type = "cccc”
case "htr”:
element.Type = "com"
case "no":
element.Type = "jnodejs"
case "jdb”:
element.Type = "tomcat"
}
}
This is the struct
type FTR struct {
Id string
Mod []Mod
}
type Mod struct {
Name string
Type string
}
Solution
You can either use pointers:
type FTR struct {
Id string
Mod []*Mod
}
Or address your items directly
for i := range ftr.Mod {
switch ftr.Mod[i].Type {
case "aaa", "bbbb":
ftr.Mod[i].Type = "cccc"
case "htr":
ftr.Mod[i].Type = "com"
case "no":
ftr.Mod[i].Type = "jnodejs"
case "jdb":
ftr.Mod[i].Type = "tomcat"
}
}
Answered By – JimB
Answer Checked By – Senaida (GoLangFix Volunteer)