jarno revisou este gist . Ir para a revisão
1 file changed, 25 insertions
slicePush.go(arquivo criado)
| @@ -0,0 +1,25 @@ | |||
| 1 | + | package main | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "fmt" | |
| 5 | + | ) | |
| 6 | + | ||
| 7 | + | func main() { | |
| 8 | + | slc := []string{"a", "b", "c", "d", "e", "f", "g", "h"} | |
| 9 | + | itemsToPush := []string{"i", "j", "k", "l"} | |
| 10 | + | fmt.Println(slc) | |
| 11 | + | slicePush(slc, itemsToPush) | |
| 12 | + | fmt.Println(slc) | |
| 13 | + | } | |
| 14 | + | ||
| 15 | + | func slicePush(a, b []string) { | |
| 16 | + | for ii := range b { | |
| 17 | + | for i := 0; i < len(a); i++ { | |
| 18 | + | if i+1 < len(a) { | |
| 19 | + | a[i] = a[i+1] | |
| 20 | + | } else { | |
| 21 | + | a[i] = b[ii] | |
| 22 | + | } | |
| 23 | + | } | |
| 24 | + | } | |
| 25 | + | } | |
Próximo
Anterior