How to return all value out the for loop in golang

Issue

In python I can return how value what I append in the array

valuesinPy= []
for i range(len(value)) :
   valuesinPy.append(value[i])
return valuesinPy 
  

It’s able to return all value I want to append in golang

valueappend =make(map[strig]string)
for i :=range value{
    valueappend['abs']=value[i]
}
return  valueappend  

it’s all same
But the value return same I got confused

Problem fix
I use the map [string]interface{}{} I less one struct of slice

valueappend:=map[string]interface{}{}
for i :=range value{
   valueappend= append(valueappend, map[string]interface{}{
        "valueIwant append" value[i] :
     }
}
return valueappend

Solution

You’ll probably need to change the value of the map each time you go through the loop, or else every value from value[i] will keep writing to that same field in the map, leaving with the last value of i as your only value in the map

Answered By – Grant

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.