Solve the "Does not support index" error in the Go program

If you are reading this article, you may be searching on Google for how to solve this problem: you are passing a pointer tosliceormapIn a function and when referencing an item*variable[0], You will receive the error.

How can I solve it?

The solution is simple: no need to use

*variable[0]

use

(*variable)[0]

Why am I receiving this strange error? 🤔

*variable[0]The Go compiler interprets it as*(variable[0]). Therefore, what you need to tell the compiler to do is to get the first element in the slice or the map item with key 0, and then dereference the pointer.

This illustrates the error:variableIn this case, it is a pointer, not a value, so you cannot get the [0] item of the pointer to the address, you need to dereference it first to get the value, which is what I think you are trying First place.


More tutorials: