I am trying to find out, how slice works. Look at the following code
package main
import "fmt"
type ByteSlice []byte
func (p *ByteSlice) Append(data []byte) {
slice := *p
*p = append(slice, data...)
}
func main() {
var a ByteSlice
a[0] = 3
b := []byte{4,6,7}
a.Append(b)
fmt.Println(b)
}
As you can see in the append function, i try to append slice parameter into call but i've got the following error:
panic: runtime error: index out of range
goroutine 16 [running]:
runtime.panic(0x4a14e0, 0x51333c)
c:/go/src/pkg/runtime/panic.c:279 +0xe9
main.main()
D:/Go/samples/src/method.go:16 +0x243
goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
c:/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
c:/go/src/pkg/runtime/proc.c:1445
goroutine 18 [runnable]:
bgsweep()
c:/go/src/pkg/runtime/mgc0.c:1976
runtime.goexit()
c:/go/src/pkg/runtime/proc.c:1445
goroutine 19 [runnable]:
runfinq()
c:/go/src/pkg/runtime/mgc0.c:2606
runtime.goexit()
c:/go/src/pkg/runtime/proc.c:1445
exit status 2
What do i here wrong?