I have the following code for implementing a splice (that is, given a byte slice full, another byte slice part, and an int pos representing the position in full that I want to overwrite with part):
package main
import (
"fmt"
"bytes"
)
func main() {
full := []byte{0,0,0,0,0,0,0}
part := []byte{1,1,1}
newFull1 := splice(full, part, 2)
fmt.Println(newFull1)
// [0 0 1 1 1 0 0]
newFull2 := splice(full, part, 3)
fmt.Println(newFull2)
// [0 0 0 1 1 1 0]
}
func splice(full []byte, part []byte, pos int) []byte {
return bytes.Join([][]byte{full[:pos], part, full[len(full[:pos])+len(part):]}, []byte{})
}
Basically, my method does a join of 3 byte slices: the first part of full that doesn't get overwritten by part, all of part, and then the remaining part of full. Is there a better/more idiomatic way of doing this? I wasn't able to find a method that implemented this in the standard library.
If you know part is completely within the bounds of full, you can use the copy function.
func main() {
full := []byte{0, 0, 0, 0, 0, 0, 0}
part := []byte{1, 1, 1}
copy(full[2:], part)
fmt.Println(full)
}
That overwrites full though. If you wanted to preserve the original, you could make a copy first with the append function.
func main() {
full := []byte{0, 0, 0, 0, 0, 0, 0}
part := []byte{1, 1, 1}
newFull := append([]byte{}, full...)
copy(newFull[2:], part)
fmt.Println("newFull: ", newFull)
fmt.Println("original full:", full)
}
Note that this still has the limitation of your original code that part must fit within the bounds of full.
Why not use the append
built-in?
func splice(full, part []byte, pos int) (ret []byte) {
ret = append(full[:pos], part...)
return append(ret, full[pos:]...)
}
This may not be very fast (lots of copying), but it's pretty readable.