Type *[]int does not support indexing!

see this simple code

    package main

    import (
    	"fmt"
    	"reflect"
    )

    func main()  {
    	var s1 []int = []int{1, 2, 3, 4}
    	fmt.Println("Value:", s1, "\n",
    		 	 "Type:", reflect.TypeOf(s1), "\n")

    	var s2 = &s1
    	s2[1] = 2333
    	fmt.Println("Value:", s2, "\n",
    		         "Type:", reflect.TypeOf(s2), "\n")

    }

when i run this i get

invalid operation: s2[1] (type *[]int does not support indexing)

but this works fine

package main

import (
	"fmt"
)

func main()  {
	a := [...]int{1, 2, 3}
	b := a
	b[1] = 5
	fmt.Println(a)
	fmt.Println(b)

}

it gives

[1 2 3]
[1 5 3]

so why is this ??

Do this:

	(*s2)[1] = 2333

see https://play.golang.org/p/2lCGtCEx32U

1 Like

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