https://go.dev/ref/spec#Slice_expressions

> For slices, the upper index bound is the slice capacity cap(a) rather
than the length.

- sean

On Sat, Jan 25, 2025, 17:20 Pyrode <pyrode...@gmail.com> wrote:

> Hey,
>
> The following code <https://go.dev/play/p/P2gPMHKuJW8> allows elements
> outside the length of the slice to be accessed.
> Is this the expected behaviour or a bug?
>
> Thanks
> ```
> package main
>
> import "fmt"
>
> func main() {
> foo([]int{1, 2, 3, 4}[0:1])
> }
>
> func foo(bar []int) {
> fmt.Println(len(bar)) // Output: 1
> fmt.Println(bar[0:3]) // Output: [1,2,3]
> }
> ```
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/71c52f83-a956-449d-8e5b-adf1a9ebfbb4n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/71c52f83-a956-449d-8e5b-adf1a9ebfbb4n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPpBHv9sA2_gmXBmAGbWm-ugahNGgkwxFx9QTqmaLfswHg%40mail.gmail.com.

Reply via email to