Understanding "slices" and its output

go version go1.7.4 windows/amd64
** Windows 10 64 bit**

Executed / Ran code related with slices, that is present with tour on page 7/27

I first raised a concern about the behavior that I assume is erroneous, under tour Issues, as I observed these while using the tour (issue #133)

golang/go informed me to share my observation on this website.

As a user / coder, I expect to use a syntax that is more generic. I expect that if both the inputs are of same type, then their permitted first value should be same. Please refer to example where I have used N and M to discuss the issue.

Observation & Issue details below:

primes := [6]int{2, 3, 5, 7, 11, 13}

used in “slices” explanation present on “tour” at location / url / Context: http://127.0.0.1:3999/moretypes/7

Various observations:

Observation 1:
Let us assume we want to slice value from “primes” array. Hence, we will write some thing like below, with N and M being two integer numbers:

var s[]int = primes[N:M]

where:
N = Low bound = Position of the first number to be copied to slice. Here we use C style reference, hence the position count starts from 0
M = Higher bound
There is however confusion about what value of M should be used:
i. M can be a numeric number that informs how many values need to be copied
ii. M can be the position of the last number that should be a part of the slice:
a. assuming that the first value in the array is at position 0
b. assuming that the first value in the array is at position 1

Based on shared example (shared on tour), the possible description for M is ii.b

Queries:
A. Why cannot N and M count from 0? Why does N start count from 0 and M from 1?
B. Why cannot we use M as the number of elements we want to keep in slice?

Observation 2:
Also why error is not thrown for “var s[]int = prime[6:6]” (as the permitted values for low bound (i.e. N) are 0 through 5)?

Observation 3:
Printing value of “var s []int = primes[4:4]” prints [] and does not throw error. Is there a reason to come up with this output? How can one compare output or know that slice has no valid data? I am looking for a conditional / if check.

The details / information provided in slices section should elaborate on above observations. Also in case the Observations 2 and 3 are some kind of bug, then they must be fixed.

Slices can be difficult to understand. They look like arrays in other languages, but act a bit differently.

Why cannot N and M count from 0? Why does N start count from 0 and M from 1?

Both N and M start from 0. The correct relation is 0 <= N <= M <= len(primes)

len is introduced in step 11 of that part of the tour.

Why cannot we use M as the number of elements we want to keep in the slice?

The number of elements in the slice is M-N and can be determined with len(primes).

Also why error is not thrown for var s []int = primes[6:6](as the permitted values for low bound (i.e. N) are 0 through 5)?

An error is not thrown because there is not an error. The maximum value for N and M is len(primes), which is 6.

Printing value of var s []int = primes[4:4] prints [] and does not throw error. Is there a reason to come up with this output?

How can one compare output or know that slice has no valid data? I am looking for a conditional / if check.

if len(primes) > 0 { // has data }

This assumes that no invalid data has been put into the slice.

Further Reading

2 Likes

Thanks Nathan Kerr for explanation.

I would like to close this issue, as my queries are addressed.

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