I want to have a slice of strings, and when certain strings are found, they are replaced by a group of related strings.
For instance, if I have this :
[]string{"A","FROM_B_TO_E","F"}
After my method runs I want to have :
[]string{"A","B","C","D","E","F"}
I came up with this code, the thing is, although I can print my to_be_added slice just before actually adding it, for some reason it does not work. It does work however if I change my translateRule so instead of returning a slice of string it only returns a single string :
func groupRules(validationRules []string){
for index,rulename := range validationRules {
if succeeded, to_be_added := translateRule(rulename) ; succeeded == true{
fmt.Println("Entro! ", to_be_added)
validationRules = append(append(validationRules[:index],to_be_added...), validationRules[index+1:]...)
}
}
}
func translateRule(rule string) ( bool , []string ) {
if rule == "rs_full" {
return true,[]string{"sapo","rana"}
}
return false,nil
}
So, my lack of Go experience or the bad code I write lead me to this :
func groupRules(validationRules []string) []string{
var tmp_slice []string
for _ ,rulename := range validationRules {
if succeeded, to_be_added := translateRule(rulename) ; succeeded == true{
tmp_slice = append(tmp_slice,to_be_added...)
}else{
tmp_slice = append(tmp_slice,rulename)
}
}
return tmp_slice
}
func translateRule(rule string) ( bool , []string ) {
if rule == "rs_full" {
return true,[]string{"sapo","rana","tigre"}
}
return false,nil
}
Now it works flawlessly.
Thank you all.