sontags
sontags

Reputation: 3241

Pointers and debugging in golang

I'm stuck in a situation and cannot figure out what I messed up. Easiest way to explain is probably some minimal example: http://play.golang.org/p/14lbOBsCCo

I am tying to modify a value of a struct via its pointer but end up modifing some memory other the part I want. Line 92 is where my issue is.

How would you debug a situation like this (tools etc.), and how do I get the broker.Port set?

Thanks for hints/suggestions!

Upvotes: 2

Views: 601

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109475

You're not using pointers throughout. Start off with a Registry of type:

type Registry []*Broker

and work from there

Working example

As far as debugging tricks, this was my process:

  • Value isn't being changed, so something is being copied by value
  • Notice that Registry is type []Broker, but we want to modify Brokers, so it needs to be a pointer
  • Change type Registry to []*Broker
  • Keep attempting to compile, letting the compiler tell me every place we are using a value where we need a pointer (woohoo fast compile times and static typing)

Upvotes: 6

Related Questions