Skip to content

Viper library (Go) fails to unmarshal yaml config file

workplace with laptop and ceramic vase with basket

In times when you’re trying to unmarshal a config file using viper, check whether your config file has nested yaml structures.

  api_key: some_api_key
      api_key: some_api_key

As shown in the example above, there are multiple layers of structs under providers (i.e. payment contains dwolla). The different with service is that its fields are set only in one level.

If your config file is a single level struct, then there won’t be any problems using the yaml Golang structural flag. However, if your config file uses multiple-level structs as in the case of providers, then you need to use mapstructure instead of yaml when defining your structs.

See this stack overflow resource for more information.

I’ll build out a small reproducible repository to demonstrate this later.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.