snippetgoCritical
How to prepend int to slice
Viewed 0 times
prependhowsliceint
Problem
I have a slice which I created using
I want to prepend an integer to this slice, something like
but obviously it won't work since append needs a slice as the first argument.
I have tried this but it only works for strings and it's not working in my case.
var x []int;
for i := 2; i < 10; i += 2 {
x = append(x, i);
}I want to prepend an integer to this slice, something like
x = append(2, x)but obviously it won't work since append needs a slice as the first argument.
I have tried this but it only works for strings and it's not working in my case.
Solution
Use a slice composite literal:
Playground: https://play.golang.org/p/Yc87gO7gJlD
Output:
However, this more efficient version may make fewer allocations, An allocation is only necessary when there is no spare slice capacity.
Playground: https://play.golang.org/p/fswXul_YfvD
Output:
Good code must be readable. In Go, we often hide implementaion details inside a function. Go compilers are optimizing compilers, small, simple functions (like
Playground: https://play.golang.org/p/wl6gvoXraKH
Output:
See Go SliceTricks.
[]int{1}, For example:package main
import (
"fmt"
)
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(x)
x = append([]int{1}, x...)
fmt.Println(x)
}Playground: https://play.golang.org/p/Yc87gO7gJlD
Output:
[2 4 6 8]
[1 2 4 6 8]However, this more efficient version may make fewer allocations, An allocation is only necessary when there is no spare slice capacity.
package main
import (
"fmt"
)
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(x)
x = append(x, 0)
copy(x[1:], x)
x[0] = 1
fmt.Println(x)
}Playground: https://play.golang.org/p/fswXul_YfvD
Output:
[2 4 6 8]
[1 2 4 6 8]Good code must be readable. In Go, we often hide implementaion details inside a function. Go compilers are optimizing compilers, small, simple functions (like
prependInt) are inlined.package main
import (
"fmt"
)
func prependInt(x []int, y int) []int {
x = append(x, 0)
copy(x[1:], x)
x[0] = y
return x
}
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(len(x), cap(x), x)
x = prependInt(x, 1)
fmt.Println(len(x), cap(x), x)
}Playground: https://play.golang.org/p/wl6gvoXraKH
Output:
4 4 [2 4 6 8]
5 8 [1 2 4 6 8]See Go SliceTricks.
Code Snippets
package main
import (
"fmt"
)
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(x)
x = append([]int{1}, x...)
fmt.Println(x)
}[2 4 6 8]
[1 2 4 6 8]package main
import (
"fmt"
)
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(x)
x = append(x, 0)
copy(x[1:], x)
x[0] = 1
fmt.Println(x)
}[2 4 6 8]
[1 2 4 6 8]package main
import (
"fmt"
)
func prependInt(x []int, y int) []int {
x = append(x, 0)
copy(x[1:], x)
x[0] = y
return x
}
func main() {
var x []int
for i := 2; i < 10; i += 2 {
x = append(x, i)
}
fmt.Println(len(x), cap(x), x)
x = prependInt(x, 1)
fmt.Println(len(x), cap(x), x)
}Context
Stack Overflow Q#53737435, score: 202
Revisions (0)
No revisions yet.