0

Consider the following issue.

I have two structs, Graph and Vertex

package main

import (
  "github.com/shopspring/decimal"
)

type Graph struct {
  vertexes map[string]Vertex
}

type Vertex struct {
  key string
  edges map[string]decimal.Decimal
}

and a reference receiver for Vertex

func (v *Vertex) Edge(t string, w decimal.Decimal) {
  v.edges[t] = w
}

I would like to update at various times the values of the Vertex.edges map inside the Graph struct.

I initially tried this code, coming from Python:

func UpdateGraph(person, friend, strength string, g *Graph) decimal.Decimal {
  nicestrength, _ := decimal.NewFromString(strength)
  g.vertexes[person].Edge(friend, nicestrength)
  return nicestrength
}

func main() {
  people := []string{"dave", "tim", "jack"}
  g := Graph{make(map[string]Vertex)}
  for _, p := range people {
    g.vertexes[p] = Vertex{p, make(map[string]decimal.Decimal)}
  }
  UpdateGraph("dave", "jack", "0.3434555444433444", &g)
}

I get

# command-line-arguments
./main.go:28:19: cannot call pointer method on g.vertexes[person]
./main.go:28:19: cannot take the address of g.vertexes[person]

So I tried amending g.vertexes[person].Edge(friend, strength) to:

pToVertex := &g.vertexes[person]
pToVertex.Edge(friend, nicestrength)

And now receive

./main.go:26:16: cannot take the address of g.vertexes[person]

What's the solution to this?

YES, this question has been asked but as far as I can see there have only been answers which explain why it is the case. Now I understand why, how do I solve my problem?

Callam Delaney
  • 641
  • 3
  • 15
  • 3
    Possible duplicate of [Why does go forbid taking the address of (&) map member, yet allows (&) slice element?](https://stackoverflow.com/questions/32495402/why-does-go-forbid-taking-the-address-of-map-member-yet-allows-slice-el) – Kevin M Granger May 10 '18 at 00:12
  • 2
    Long story short, you can't take the address of a map member. Check out the link above to find out why and what to do instead. It might just be easier to make the map value type a pointer to a Vertex. – Kevin M Granger May 10 '18 at 00:12
  • 1
    Possible duplicate of https://stackoverflow.com/questions/20224478/dereferencing-a-map-index-in-golang – Charlie Tumahai May 10 '18 at 00:45
  • There are duplicates. No answer I can find provides an actual solution, only an explanation as to why you cannot take the address of a map. I am asking how do I do this, not why. – Callam Delaney May 10 '18 at 09:33

1 Answers1

1

Two options:

  1. Change the Edge method so that it doesn't use a pointer receiver:

    func (v Vertex) Edge(t string, w decimal.Decimal) {
      v.edges[t] = w
    }
    

    Because edges is a map, and maps are pointers behind the scenes, updates will be propagated despite the non-pointer receiver

  2. Store Vertex pointers:

    type Graph struct {
      vertexes map[string]*Vertex
    }
    
Caleb
  • 9,272
  • 38
  • 30