Nick's Blog

When to use map vs struct in golang

The time to use a Map:

  1. All keys must be the same type
  2. All values must be the same type
  3. Keys are indexed – we can iterate over them
  4. Use to represent a collection of related properties
  5. Don’t need to know all the keys at compile time
  6. Reference Type

The time to use a struct

  1. Values can be different type
  2. Keys don’t support indexing
  3. You need to know all the different fields at compile time
  4. Use to represent a “thing” with a lot of different properties
  5. Value Type

Credits: Master the fundamentals and advanced features of the Go Programming Language (Golang) from Stephen Grider