0

I'm currently trying to learn GO and mainly knowing and working with Java, ASP.Net and some Python, there is no experience working with C-like pointers, which causes my current confusion.

A library I'm currently using to write my first GO project is called Commando. There I have the struct CommandRegistry and the variable of interest is called Commands.

In the struct the variable is described as the following:

// registered command configurations
Commands map[string]*Command

On a first glimpse I would understand this as a Map object containing a list of Strings, however it also shows the pointer reference to the actual Command object.

All I can see is that it is a map I can loop over which returns the name of the command ( the string ), however I'm wondering if the *Command in the type description means I can somehow dereference the pointer and retrieve the object itself to extract the additional information of it.

As I know the & operand is used to create a new pointer of another object. Pass-by-reference basically instead of pass-by-value. And the * operand generally signals the object is a pointer or used to require a pointer in a new function.

Is there a way I can retrieve the Command object or why does the type contain the *Command in it's declaration?

5
  • 2
    it's a map, so you can get the value for your string key Commands["key"] should give you the pointer to your Command. In Java this would be declared as Map<String, Command> - without pointers. Commented Oct 21, 2021 at 10:37
  • 4
    Commands is a map, it maps from string values to *Command, that is, pointer to Command. Indexing the map will give you the associated *Command pointer, e.g. Command["foo"]. You can dereference that (if it's not nil). You can also iterate over the map with a loop, getting all key-value pairs, like this: for s, cmd := range Commands { fmt.Printf("key: %s, value: %v", s, cmd) } Commented Oct 21, 2021 at 10:38
  • gobyexample.com/range Commented Oct 21, 2021 at 10:39
  • Thanks a lot for the quick comments. Indeed it seems I was blinded by the new topic of pointers and overlooked the simple solution that it still is a map just declared differently. Commented Oct 21, 2021 at 10:41
  • 2
    Note that you don't have to dereference the pointer in most cases, you can access the struct's fields and call it's methods using the pointer, it is basically short-hard for dereferencing so that you don't have to do it every time. Commented Oct 21, 2021 at 10:41

1 Answer 1

2

Commands is a map (dictionary) which has strings as keys, and pointers to Commands as values. By passing it a key, you will get a pointer to the command it belongs to. You can then dereference the pointer to an actual Command object by using the * operator. Something like dereferencedCommand := *Commands["key"].

The * operator can be quite confusing, at least it was for me. When used as a type it denotes that we are receiving the memory address of some variable. But to dereference a memory address to a concrete type, you also use the * operator.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the answer and explanation of the * operator. It indees is a bit confusing when working with it for the first type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.