Global map initialization

http://play.golang.org/p/WC8iRPmj3G

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:
http://play.golang.org/p/CBLZlxd0wB

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:
http://play.golang.org/p/ocjkvshRfK

so the main function looks like:

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

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

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:
https://tour.golang.org

map literals are explained there: https://tour.golang.org/moretypes/17

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

-s

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.