Global map initialization

I have a simple map. Previously it was:

  • declared at the module level,
  • created in main using make at runtime, and then
  • initialized with a series of assignments.

Now I initialize it all at once with a single block of data, but to do this I need two map variables: one at the module level and one in main.

Am I missing something, or is this just the way of Go?

this should help:

Why not initialize it directly at the package level, if that’s the desired end result?

and here is a slightly more Go-idiomatic solution, defining a type Roman int implementing the Stringer interface:

so the main function looks like:

func main() {
	rom1 := RomanFrom("CXXIII")
	rom2 := RomanFrom("CCCXXI")

	fmt.Printf("%v + %v = %v\n", rom1, rom2, rom1+rom2)

I tried using an equal sign in six different places, apparently all wrong. Who knew Go would be so picky?

you may want to follow the go-tour:

map literals are explained there:

the tour is just a couple of hours for somebody already knowledgeable in programming.


I tried the tour. I quickly tired of it. I find programming exercises more enlightening. Stupid syntax errors keep tripping me up.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.