panic: runtime error: slice bounds out of range - go

I'm following this tutorial: https://gobyexample.com/slices
I was in the middle:
package main
import "fmt"
func main() {
s := make([]string, 3)
fmt.Println("emp:", s)
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
c := make([]string, len(s))
copy(c, s)
fmt.Println("copy:", c)
l := s[2:5]
fmt.Println("sl1:", l)
}
when I suddenly encountered this error:
alex#alex-K43U:~/golang$ go run hello.go
emp: [ ]
set: [a b c]
copy: [a b c]
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
main.main()
/home/alex/golang/hello.go:19 +0x2ba
goroutine 2 [syscall]:
created by runtime.main
/usr/lib/go/src/pkg/runtime/proc.c:221
exit status 2
What does it mean? Is the tutorial mistaken? What can I do to fix it?

Your code omits these lines from the original example:
s = append(s, "d")
s = append(s, "e", "f")
Without these lines, len(s) == 3.

You forgot the append part that grows s.
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s)

Related

Golang permutations of numbers, recursion not working

Here is the code:
package main
import "fmt"
func anon(n []int, sl []int, result [][]int) {
if len(n) == 0 {
result = append(result, sl)
fmt.Printf("result %v\n", result)
return
}
for i , _ := range n {
fmt.Printf(" n %v\n", n)
sl = append(sl, n[i])
ab := append(n[:i], n[i+1:]...)
fmt.Printf("i %v ---ab %v, sl %v, result %v ---\n",i, ab,sl,result )
anon(ab, sl , result)
}
}
func permute(nums []int) [][]int {
var sl1 = []int{}
var result = [][]int{}
anon(nums, sl1, result)
return result
}
func main() {
sl2 := []int{1,2}
permute(sl2)
}
I am expecting 'result' as [[1,2], [2,1]]. However, when I look at the below output from the code run:
n [1 2]
i 0 ---ab [2], sl [1], result [] ---
n [2]
i 0 ---ab [], sl [1 2], result [] ---
result [[1 2]]
n [2 2]
i 1 ---ab [2], sl [1 2], result [] ---
n [2]
i 0 ---ab [], sl [1 2 2], result [] ---
result [[1 2 2]]
I see for (in bold) i=1, I have ab[2], sl[1 2], result[] and n[2,2]. I am not able to get it working for Golang. Something similar works well with Python.
Thanks for answering.
Check this complete sample, which can help you to understand better how to make a permutation with golang: https://go.dev/play/p/JKG_FtilQCz.
In Golang slices are pointers to arrays, and when you make append(n[:i], n[i+1:]...) you are adding a new value in variable n, so you are changing the initial sl2 value, that should be {1,2} but is transformed in {2,2} as you noted. Try not to append in n, instead append in ab or something else.

Capacity of a slice

I am new to Golang and I am following the Tour. The code below produces the following results:
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0]
d len=3 cap=3 [0 0 0]
How can c have the same cap as b when it was not specified at all?
package main
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n", s, len(x), cap(x), x)
}
Every slice is a pointer type that points to an underlying array. When you make b you create an underlying array to which b points. When you create c by resclicing b you will create a new slice that points to the same underlying array as b.
When you change some entries in b, you will notice that those values get changed in c, too.
There is an official blog entry, that explains all of this in detail: https://blog.golang.org/go-slices-usage-and-internals

Idiomatic Splice in Go

I checked an existing answer but it's not similar to my case.
I need to pluck an element at the index and break out of the for loop at runtime based on Compare function.
Issues:
If element to pluck is found at 0 index, index-1 will throw slice bounds of range error and similarly if index+1 is greater than len(elements).
Question: What's the best concise way to achieve the above?
for index, element := range elements {
if element.Compare() == true {
elements = append(elements[:index-1], elements[index+1:]...)
break
}
}
Attempt
for index, element := range elements {
if element.Compare() == true {
if len(elements) > 1 {
elements = append(elements[:index-1], elements[index+1:]...)
} else if len(elements) == 1 {
delete(elements, 0)
}
break
}
}
Attempt 2 Playground any improvements/suggestions?
The idea is to copy the remaining elements from beginning to index and then any elements after.
var elements = []string {"a", "b", "c", "d"}
fmt.Println(elements)
for index, element := range elements {
if element == "c" {
var temp = elements[:index]
for i := index + 1; i<len(elements); i++ {
temp = append(temp, elements[i])
}
elements = temp
break
}
}
fmt.Println(elements)
The high index in a slice expression is exclusive.
This means your example is flawed, and also that no special treatment is required.
The correct slicing expression is:
elements = append(elements[:index], elements[index+1:]...)
If index is the first element (0), then elements[:0] will be an empty slice.
If index is the last element (len-1), then elements[index+1:] will also be an empty slice, as index+1 will be equal to the lenght of the slice. So the solution is simply:
for index, element := range elements {
if element.Compare() {
elements = append(elements[:index], elements[index+1:]...)
break
}
}
To demonstrate it on the Go Playground, let's substitute the Compare() method with a simple index check:
for _, idxToRemove := range []int{0, 2, 4} {
s := []int{0, 1, 2, 3, 4}
for i := range s {
if i == idxToRemove {
s = append(s[:i], s[i+1:]...)
break
}
}
fmt.Println(idxToRemove, ":", s)
}
Output (try it on the Go Playground):
0 : [1 2 3 4]
2 : [0 1 3 4]
4 : [0 1 2 3]
If the slice s is sorted and len(s) is large, find x using a binary search. For example,
package main
import (
"fmt"
"sort"
)
func pluck(s []string, x string) []string {
i := sort.SearchStrings(s, x)
if i >= 0 && i < len(s) && s[i] == x {
s = append(s[:i], s[i+1:]...)
}
return s
}
func main() {
s := []string{"a", "b", "c", "d"}
fmt.Println(s)
s = pluck(s, "b")
fmt.Println(s)
}
Output:
[a b c d]
[a c d]
If the order of slice s does not need to be preserved, switch elements. For example,
package main
import "fmt"
func pluck(s []string, x string) []string {
for i, v := range s {
if v == x {
s[i] = s[len(s)-1]
s = s[:len(s)-1]
break
}
}
return s
}
func main() {
s := []string{"a", "b", "c", "d"}
fmt.Println(s)
s = pluck(s, "b")
fmt.Println(s)
}
Output:
[a b c d]
[a d c]
Otherwise, splice slice s elements. For example,
package main
import "fmt"
func pluck(s []string, x string) []string {
for i, v := range s {
if v == x {
s = append(s[:i], s[i+1:]...)
break
}
}
return s
}
func main() {
s := []string{"a", "b", "c", "d"}
fmt.Println(s)
s = pluck(s, "b")
fmt.Println(s)
}
Output:
[a b c d]
[a c d]
I'm not sure if this is idiomatic, but this works quite well:
package main
import "fmt"
func splice(start, count int, items []string) (ret []string) {
ret = make([]string, len(items)-count)
copy(ret, items[:start])
copy(ret[start:], items[start+count:])
return
}
func main() {
s := []string{"a", "b", "c", "d"}
fmt.Println(s)
s = splice(1, 2, s)
fmt.Println(s)
}
Go Playground: https://play.golang.org/p/UNtdtw77sEQ

Incorrect values inside goroutines when looping

I have read through CommonMistakes as well as run my code through the -race flag, but I can't seem to pinpoint what is wrong here:
package main
import (
"fmt"
)
func main() {
i := 1
totalHashFields := 6
for i <= totalHashFields {
Combinations(totalHashFields, i, func(c []int) {
fmt.Println("Outside goroutine:", c)
go func(c []int) {
fmt.Println("Inside goroutine:", c)
}(c)
})
i++
}
}
func Combinations(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
(The Combinations function is a combinations algo for those interested)
Here is some of the output from fmt.Println:
Outside goroutine: [0 1 4]
Inside goroutine: [5 5 5]
Outside goroutine: [0 1 2 3 4 5]
Inside goroutine: [5 5 5 5 5 5]
Basically, even though I'm passing c as a parameter to my anonymous go function, the value is consistently different to the value outside of this scope. In the output above, I expected the 2 "Inside" values to also be [0 1 4] and [0 1 2 3 4 5], respectfully.
The problem is that you goroutines all work on distinc int slices but these share a common backing array: After completing Combinations the slice s will be full of 5s. Your c in main shares the underlying backing array with s.
But your goroutines do not start executing until Combinations is done so once they do start, the will see the final value of s which is just 5s.
Here it does not help to pass in the slice like you did as this makes a proper copy of c but not of the backing array.
Try
Combinations(totalHashFields, i, func(c []int) {
fmt.Println("Outside goroutine:", c)
cpy := make([]int, len(c))
copy(cpy, c)
go func(c []int) {
fmt.Println("Inside goroutine:", c)
}(cpy)
})
to make a "deep copy" of c.

Best way to swap variable values in Go?

Is it possible to swap elements like in python?
a,b = b,a
or do we have to use:
temp = a
a = b
b = temp
Yes, it is possible. Assuming a and b have the same type, the example provided will work just fine. For example:
a, b := "second", "first"
fmt.Println(a, b) // Prints "second first"
b, a = a, b
fmt.Println(a, b) // Prints "first second"
Run sample on the playground
This is both legal and idiomatic, so there's no need to use an intermediary buffer.
Yes it is possible to swap elements using multi-value assignments:
i := []int{1, 2, 3, 4}
fmt.Println(i)
i[0], i[1] = i[1], i[0]
fmt.Println(i)
a, b := 1, 2
fmt.Println(a, b)
a, b = b, a // note the lack of ':' since no new variables are being created
fmt.Println(a, b)
Output:
[1 2 3 4]
[2 1 3 4]
1 2
2 1
Example: https://play.golang.org/p/sopFxCqwM1
More details here: https://golang.org/ref/spec#Assignments
Yes you can swap by using
a, b = b, a
So if a = 1 and b= 2,
then after executing
a , b = b, a
you get a = 2 and b = 1
Also, if you write
a, b, a = b, a, b
then it results b = 1 and a = 2
There is a function called Swapper which takes a slice and returns a swap function. This swap function takes 2 indexes and swap the index values in the slice.
package main
import (
"fmt"
"reflect"
)
func main() {
s := []int{1, 2, 3}
fmt.Printf("Before swap: %v\n", s)
swapF := reflect.Swapper(s)
swapF(0, 1)
fmt.Printf("After swap: %v\n", s)
}
Try it
Output
Before swap: [1 2 3]
After swap: [2 1 3]
Yes, you can swap values like python.
a, b := 0, 1
fmt.Printf("Before swap a = %v, b = %v\n", a, b)
b, a = a, b
fmt.Printf("After swap a = %v, b = %v\n", a, b)
Output
Before swap a = 0, b = 1
After swap a = 1, b = 0
you can use ^ option like this...
func swap(nums []int, i, j int) {
nums[i] ^= nums[j]
nums[j] ^= nums[i]
nums[i] ^= nums[j]
}

Resources