Kubernetes Dependencies To Use In Your Go Application

In Go, we often try to use exclusive depend only on the built-in packages as much as possible. It's a sign of pride in Go to avoid adding a dependency by cleverly using a built-in Go package.

The built-in packages however, like Go's flag package, can often be overly simplistic, and do not reach the expectations of most users. If you find yourself looking for a package that can achieve more than the built-in package, and is also trusted enough in the community that it's used by Kubernetes, you have found the right article.

Cobra, create modern CLI applications - Github

If you simply want to add a flag to your CLI app, then the built-in flag package would suffice. Once you want more though, it starts to show its limitations, so many that someone's even created a POSIX/GNU-style flag drop-in replacement.

With cobra, you could have commands and sub-commands in a single Go file. You'd create the root command with:

rootCmd = &cobra.Command{
    Use:   "cobra",
    Short: "A generator for Cobra based Applications",
}

And then add sub-commands:

rootCmd.AddCommand(&cobra.Command{
  Use:   "version",
  Short: "Print the version number of Hugo",
  Long:  `All software has versions. This is Hugo's`,
  Run: func(cmd *cobra.Command, args []string) {
    fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
})

Check their Github page for more examples.

Viper, configuration solution - Github

It's not uncommon to need a configuration file, so let's say you store all your configuration in a config.yaml. But then:

  1. Later, you might want to use flags instead.
  2. You might want to change the format at some point
  3. Then you decide that want to set defaults for the configurations
These issues are easily solved by Viper, as opposed to only importing the YAML package for your configuration.

So let's configure our flexible configuration with Viper:

viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/appname/")   // path to look for the config file in
viper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search paths
viper.AddConfigPath(".")               // optionally look for config in the working directory
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))
}

You could also watch the config, to see if it changes.

viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
    fmt.Println("Config file changed:", e.Name)
})

Golang LRU, in-memory caching - Golang LRU

In a world of microservices, most apps themselves do not generally use much memory. That is because they normally offload the storage to caching solutions like Memcached and Redis. Using an in-memory cache like golang-lru in the right scenarios can greatly improve the simplicity of a service.

You'd want to use an in-memory cache if:
  • You don't need to store too much in the cache. (less than 1Gi, for example)
    (You do not want dozens of gigabytes of in-memory cache to be purged by a deployment)
  • You'd like your data to come back in its current Go structure.
    (So it doesn't have to be unmarshaled back into go structs)
  • You don't want to manage an installation of an in-memory distributed cache.

Using the package is just as trivial as any other in-memory storage:

cache, _ := lru.New(1)
cache.Add("post", "First blog post")

post, _ := cache.Get("post")
log.Printf("post = %s", post)

// The previous post now gets evicted
cache.Add("post", "Second blog post (will evict second)")
post, _ = cache.Get("post")
log.Printf("post = %s", post)

Similar articles
Improving the performance of a laravel application - from 2015
Doing development in laravel is a quick and pleasurable experience. Regardless of that, by the time you get to deploy the application, you might realise the applicat
Moving to the UK | Checklist - from 2015
And this is how I started listening to audiobooks. Honestly, I'm not a fan of listening to books, I read fast and having someone narrate the bo
Comments