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)
       }
progfan
  • 2,454
  • 3
  • 22
  • 28
  • 8
    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. – the system Feb 18 '13 at 03: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. – progfan Feb 18 '13 at 03:11
  • 1
    What does "trouble" mean? You get some kind of compile error? It doesn't produce the correct output? – newacct Feb 18 '13 at 08:09

2 Answers2

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.

Sonia
  • 27,135
  • 8
  • 52
  • 54
  • Thank you for your response. I shall keep your pointers in mind! – progfan Feb 18 '13 at 19:40
  • 1
    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. – Charlie Parker Feb 22 '14 at 05:32
  • 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. – MrMesees Nov 04 '20 at 08:13
8

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.

Community
  • 1
  • 1
Aleš
  • 8,896
  • 8
  • 62
  • 107