Help understanding why last character of a slice of a string doesn't print

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

[3 5 7]
My expertise is Ada language, why isn’t the printing results [3 5 7 11]?
Why is the last element of the array slice not output in print?

Slices in Go are source[starting_index : ending_index+1] conceptually:

var target []int

for i := starting_index; i < ending_index; i++ {
    target = append(target, source[i])

1 Like

Sean, Thank you Sir! That makes the engine under the hood perfectly clear to me.

No problem! While we’re mentioning “the engine,” I just want to be explicit: My conceptual example actually creates a copy but slicing doesn’t really create a copy, it adjusts the underlying array pointer, length and capacity.

Thanks. I’m trying to wrap my head around that. The Slices have the smell of pointers to a portion of the original variable. Am I correct in that assumption?

Absolutely. Here’s a demo:

Thank You Sean,
I know it’s late into July but I’m just now getting some time to actually sit behind a keyboard, other than at work. Slices appear to be very Python like. I’m not a Python user, just forced into using it at work every so often. But sincerely thankful for your thorough reply. I had seen your reply in May and I apparently rudely neglected to say thanks.

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