9

I am having a little trouble creating pointers to maps in Go. Can you please let me know if I am passing the map parameter correctly? It pairs integer values with structs.

 type symbol_table struct{ --- --- --- } //is the map parameter being called correctly? func TD(..., symbolMAP *map[int]symbol_table, ...){ --- --- --- } func main(){ symbolMAP:=make(map[int] symbol_table) TD(&symbolMAP) } 
3
  • 9
    Why pass a pointer? A map is already a reference type. Changes in the map will be observed from other variables. And what errors are you getting? Your question lacks inforamtion. Commented Feb 18, 2013 at 3:01
  • Thank you, I realized after I posted that the MAP is already a reference type. I have rectified that in my code. I strongly believe now that my problems lie elsewhere. I shall get back to this post when I know my problem better. Commented Feb 18, 2013 at 3:11
  • 1
    What does "trouble" mean? You get some kind of compile error? It doesn't produce the correct output? Commented Feb 18, 2013 at 8:09

2 Answers 2

12

Yes, you are passing it correctly, although not idiomatically. As the system pointed out, passing the map itself rather than a pointer to it is almost always better.

A comment on the question as a whole now, Rahul, you should not "get back to this post later." That is not the way to use stackoverflow. The question you asked was relatively simple ("Can you please let me know if I am passing the map parameter correctly?") and you provided enough information with your sample code to allow a simple answer such as the one I just gave. You should accept an answer, or, if you horribly regret asking your question, delete the question entirely.

You alluded to other questions you might have. This is fine, but they are not here and there is no reason to leave this question in an unanswered state. When you have composed your new questions, post them as new and separate questions.

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

3 Comments

Thank you for your response. I shall keep your pointers in mind!
why is better to pass the map itself better? The map I have is a state in a struct and it's important that it gets updated, but I want to be able to choose which map I pass, so passing a pointer to the map that I want to update is important.
You tell someone they are not doing something idiomatically, but fail to give an example of what that would look like. Most of the "answer" is a rant demanding it be accepted despite no code being present.
9

As already noted in the comments, there is no need to pass pointer to a map.

A map is already a reference type. Changes in the map will be observed from other variables.

See also Q/A: Go - Pointer to map.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.