19

When I run the below code, I get the compiler error saying that indexing is not supported.

txs := make([]string, 2)
txs[0] = "A"

p := &txs

fmt.Println(p[0])

I'm trying to understand why indexing on the slice pointer is not supported. I can make a copy of the pointer variable to value variable and then do indexing, but I'm curious to understand why the language is not supporting the indexing on slice pointer; it'd be so much convenient to do so. or is there a way to do it that i'm not aware? please let me know your thoughts.

srini
  • 1,110
  • 3
  • 11
  • 20
  • 1
    Related / possible duplicate of [Slicing a slice pointer passed as argument](http://stackoverflow.com/questions/38013922/slicing-a-slice-pointer-passed-as-argument) – icza Jul 19 '16 at 20:53
  • and also http://stackoverflow.com/questions/28709254/how-to-access-elements-from-slice-using-index-which-is-passed-by-reference-in-go – JimB Jul 19 '16 at 20:54
  • indexing a slice pointer isn't allowed, just like most other operators aren't valid for pointers. The type of the pointer is separate type from the underlying slice. – JimB Jul 19 '16 at 20:56
  • @JimB Note that indexing a pointer to array is allowed. See my linked answer. – icza Jul 19 '16 at 20:58
  • @icza: yes thanks, I always forget about that one – JimB Jul 19 '16 at 21:01

2 Answers2

45

Write (*p) to dereference the pointer p:

package main

import (
    "fmt"
)

func main() {
    txs := make([]string, 2)
    txs[0] = "A"
    p := &txs
    fmt.Println((*p)[0])
}

Playground: https://play.golang.org/p/6Ex-3jtmw44

Output:

A
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • Just what the doctor ordered. Thanks for that awesome example! I was stuck at that. – farhany Oct 07 '21 at 05:12
  • The OP specifically asked why indexing on the pointer doesn't work. This answer doesn't explain why. – jschmitter Mar 25 '22 at 20:44
  • @jschmitter the reason is obvious. `p` is a pointer, it is not a slice and thus can't be indexed. And he clearly explains "Write (*p) to **dereference** the pointer p" – alramdein May 31 '22 at 22:31
3

There's an abstraction happening there and the language designer chose not to apply it to the pointer. To give some practical reason, this is likely due to the fact that the pointer doesn't point to the beginning of an array (like the block of memory. If you're familiar with indexing this is generally done with something like startingAddress + index * sizeof(dataType)). So when you have the value type, it's already providing an abstraction to hide the extra layer of indirection that occurs. I assume the language authors didn't think it made sense to do this when you have a pointer to the slice object, given that points off to the actual memory that would be a pretty misleading. It already causes some confusion as is, but for a lot of developers, they probably will never realize this abstraction exists at all (like in most cases there is no noticeable difference in syntax when operating on a slice vs and array).

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115