vendredi 21 août 2015

What is a good pattern for initializing 3rd party packages in golang?

Using a 3rd party library like http://ift.tt/PvUq7V or http://ift.tt/1HjpPgR that have configuration options, what is a good practice for initializing them?

Both libraries give examples of initializing in an init() method inside the main package:

e.g.

package main

import (
  "fmt"
  "http://ift.tt/1HjpPgR"
)

func init() {
   viper.SetConfigName("config") // name of config file (without extension)
   viper.AddConfigPath("/etc/appname/")   // path to look for the config file in
   viper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search paths
   err := viper.ReadInConfig() // Find and read the config file
   if err != nil { // Handle errors reading the config file
      panic(fmt.Errorf("Fatal error config file: %s \n", err))
   }
}

But then in every test, you would have to copy/paste the common parts of your setup as the main package does not get run when tests are run.

Any good patterns that allow one to follow DRY?

Aucun commentaire:

Enregistrer un commentaire