-1

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
1
  • 1
    What do you mean “cant return”? May 9, 2022 at 2:06

2 Answers 2

1

One issue I can see is that the key you are using is same in all iterations of the loop

valueappend['abs']=value[i]

This will overwrite all previous values and you will only have the last one in the loop be saved. In the end you will get a map with just one value. I'm guessing that's not what you want here. Use can use a unique key for each iteration as follows, or something else along the same line

for i := range value {
    valueappend[i]=value[i]
}

@范紀予 please add more info and clarify what you are trying to achieve here.

0

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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.