2

I found a really strange issue with GCCGO and I was wondering if someone could explain it.

I'm trying to delete an element from a slice by index using the suggested slice tricks (https://github.com/golang/go/wiki/SliceTricks).

The following code (https://play.golang.org/p/f039m1h7Z1):

package main

import "fmt"

func main() {
    xs := []int{0,1,2,3,4}
    i := 2
    xs, xs[len(xs)-1] = append(xs[:i], xs[i+1:]...), 0
    fmt.Println(xs)
}

Works with the go compiler (go run), but when I try to compile it with

gccgo -g -static-libgcc 

I get a

panic: runtime error: index out of range 

at the line:

    xs, xs[len(xs)-1] = append(xs[:i], xs[i+1:]...), 0

Why is this happening?

Note: I'm writing xs[len(xs)-1] = 0 because in my full use case, this is actually a slice of pointers to structs, so according to the slice tricks, I need to make the final pointer null in the old slice to prevent memory leaks.

George
  • 131
  • 2
  • 8

1 Answers1

2

I believe that gccgo is correct here according to the language spec. This is https://golang.org/issue/23188.