Having trouble using a pointer to a map

4,459 views
Skip to first unread message

flyfish

unread,
Mar 22, 2011, 9:37:30 AM3/22/11
to golang-nuts
I created some test code to show what I am trying to do. I can create
a map, find keys and get the length of a map and access the value
stored at key. When I create a pointer to the map I can find keys, get
a length of map but cannot access a value stored at key. In the real
application I am playing with I return a pointer to a map created in a
function which is why I am trying to access the map through the
pointer not directly. If you compile and run as is it works, but
uncomment were the pointer tries to read the key "EST" and the line to
print the "offset1" var and an error will occur when compiling. Can
someone help me understand what I am doing wrong and how to do it
right?

Error:
test.go:32: invalid operation: ptr["EST"] (index of type *map[string]
int)

Test Code:

package main

import (
"log"
)

func main() {

// Creating a map of timezone offset values
var timezone = map[string] int {
"UTC": 0*60*60,
"EST": -5*60*60,
"CST": -6*60*60,
"MST": -7*60*60,
"PST": -8*60*60,
}

// Printing a timezone offset from the map
offset := timezone["EST"]
log.Print("Offset Information")
log.Print("-> Len of timezone: ", len(timezone))
log.Print("-> Value of offset: ", offset)
for key, _ := range timezone {
log.Print("-> Key of timezone: ", key)
}

// Creating a pointer to the map
var ptr *map[string] int
ptr = &timezone

// Printing a timezone offset from a pointer to the map
//offset1 := *ptr["EST"]
log.Print("Offset Information from pointer")
log.Print("-> Len of timezone: ", len(*ptr))
//log.Print("-> Value of offset: ", offset1)
for key, _ := range timezone {
log.Print("-> Key of timezone: ", key)
}

}

chris dollin

unread,
Mar 22, 2011, 9:43:18 AM3/22/11
to flyfish, golang-nuts
On 22 March 2011 13:37, flyfish <dsl...@gmail.com> wrote:
> I created some test code to show what I am trying to do. I can create
> a map, find keys and get the length of a map and access the value
> stored at key. When I create a pointer to the map I can find keys, get
> a length of map but cannot access a value stored at key. In the real
> application I am playing with I return a pointer to a map created in a
> function which is why I am trying to access the map through the
> pointer not directly. If you compile and run as is it works, but
> uncomment were the pointer tries to read the key "EST" and the line to
> print the "offset1" var and an error will occur when compiling. Can
> someone help me understand what I am doing wrong and how to do it
> right?

You want (*ptr)["EST"], not *ptr["EST"] (because that parses as
*(ptr["EST"])).

You probably don't want a pointer-to-map anyway; what makes you
think you do?

Chris

--
Chris "allusive" Dollin

Jan Mercl

unread,
Mar 22, 2011, 9:43:42 AM3/22/11
to golan...@googlegroups.com, flyfish
On Tuesday, March 22, 2011 2:37:30 PM UTC+1, flyfish wrote:

offset1 := (*ptr)["EST"] 

Additionally, often a pointer to a map is not necessary at all - map is already a reference type.

flyfish

unread,
Mar 22, 2011, 9:54:22 AM3/22/11
to golang-nuts
Thank you for the fast replies, in my application I am creating a map
in a function located in a different file. I assumed I then needed to
pass an address back from the function to be able to access the map
from the calling file. In short something like below except this
example is very simple. Is this incorrect?

func getMap() (timezone *map[string] int) {
var timezone = map[string] int {
"UTC": 0*60*60,
"EST": -5*60*60,
"CST": -6*60*60,
"MST": -7*60*60,
"PST": -8*60*60,
}

return &timezone
}

// Creating a global pointer to be accessed by several other places or
functions in the application code.
var ptr *map[string] int

// Getting the map in a local scope variable and then pointing the
global pointer to it for access outside of the current scope.
timezone := getMap()
ptr = timezone;



On Mar 22, 9:43 am, chris dollin <ehog.he...@googlemail.com> wrote:

chris dollin

unread,
Mar 22, 2011, 10:08:21 AM3/22/11
to flyfish, golang-nuts
On 22 March 2011 13:54, flyfish <dsl...@gmail.com> wrote:
> Thank you for the fast replies, in my application I am creating a map
> in a function located in a different file. I assumed I then needed to
> pass an address back from the function to be able to access the map
> from the calling file. In short something like below except this
> example is very simple. Is this incorrect?
>
> func getMap() (timezone *map[string] int) {
>  var timezone = map[string] int {
>    "UTC":  0*60*60,
>    "EST": -5*60*60,
>    "CST": -6*60*60,
>    "MST": -7*60*60,
>    "PST": -8*60*60,
>  }
>
>  return &timezone
> }

You can return the map, rather than a pointer to it. All copies
of a map value operate over the same mapping -- a map is
effectively already a pointer.

(Sometimes you want a pointer-to-map for the same reason as
you may want a pointer-to-pointer: to be able to update an
existing map /variable/ remotely. But that doesn't happen very
often.)

flyfish

unread,
Mar 22, 2011, 11:01:20 AM3/22/11
to golang-nuts
Thank you very much, Now I understand whats going on and am just
returning the map itself. :)
Reply all
Reply to author
Forward
0 new messages