Go Tour about slice of slice examples require corrections

Tour Go 14 and 15/27 are about slice of slice.

Samples report errors when trying to use them as they display. Slice of slices are authorized by specifications. Append does not work on an empty slice of slice.
If init is set with 1 element like board := [1][]int
then append will work on board[0]

Another solution is to add slice notation to the value added.
In this case, of course everything works.
In example 15/27, printSlice cannot work if argument is
not set to (s [][]int). Again the correction then works.

Could you confirm that slice of slice is supported ?
If yes, code should look like this in Go Tour or are maybe the mistakes in the samples on purpose ? It seems to me pretty complicated to correct the code at this stage.

Regards,

package main

import “fmt”

func main() {
var s [][]int
printSlice(s)

// append works on nil slices.
s = append(s, []int{0,})
printSlice(s)

// The slice grows as needed.
s[0] = append(s[0], 1)
printSlice(s)

// We can add more than one element at a time.
s[0] = append(s[0], 2, 3, 4)
s = append(s,  []int{2, 3, 4,}) //or add a separate slice
printSlice(s)

}

func printSlice(s [][]int) {
fmt.Printf(“len=%d cap=%d %v\n”, len(s), cap(s), s)
}

I don’t think there is a bug, slide 15/27 is not about a slice of slices, [][]int, but a normal single dimensional slice, []int

Granted that the text does not speak of slice of slice but the first line writes var s [][]int as printSlice()

Maybe the error is in this line but then it is very similar to previous chapters of the Tour.
I mention slice of slice because chapter 14 contains only “Slices can contain any type, including other slices.”

Regards,

Are we talking about the same thing ?

The text of the example is

package main

import "fmt"

func main() {
	var s []int
	printSlice(s)

	// append works on nil slices.
	s = append(s, 0)
	printSlice(s)

	// The slice grows as needed.
	s = append(s, 1)
	printSlice(s)

	// We can add more than one element at a time.
	s = append(s, 2, 3, 4)
	printSlice(s)
}

func printSlice(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

https://tour.golang.org/moretypes/15
It looks indead the same. I’m using the online Go Tour in case there is a difference. I notice that it keeps previous modifications for test purposes but I don’t remember having attempted anything on this one. Hitting the reset button reloads indeed the version that you posted. Sorry for the misunderstanding.

I wanted to use a slice of slice for some development and I just found out that the explanation that I read was incomplete. I followed up from the previous item and misunderstood the change of subject.

Although appending is not explained before, using a slice of slice from the code that I posted looks like a useful supplement.

My code using slice of slice now works and the structure is what was needed. I read again the moretypes section and I see that 18/27 exercise is based on a slice of slice. It would be better to move section 14 which introduces the subject to section 17.

Also the initialisation of the slice of slice board[][] is only with litterals in a few elements. Further, if you look up the toc of Go Tour, you end up on 14.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.