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?
Commands["key"]should give you the pointer to your Command. In Java this would be declared asMap<String, Command>- without pointers.Commandsis a map, it maps from string values to*Command, that is, pointer toCommand. Indexing the map will give you the associated*Commandpointer, e.g.Command["foo"]. You can dereference that (if it's notnil). 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) }