Useful Go Projects

I found myself starring quite a few Go projects on GitHub lately, fully aware that even if I initially starred these projects in hope I’d remember to use them if the need arose, that only sounded good in theory. I never check the projects I previously starred.

And so, to keep track of these useful Golang projects, I decided to compile a list of them in no particular order.

cobra

spf13/cobra is a library for creating modern command line applications.

This is a widely used library, in fact, even kubectl is made using cobra.

Even though Go on its own has very good native support for creating CLI applications, this library makes it not only easier, but also allows significantly more customization.

go-keyring

zalando/go-keyring is a cross-platform keyring interface for Go.

package main

import (
    "log"

    "github.com/zalando/go-keyring"
)

func main() {
    // Add secret for service "your-app-name" and the user "johndoe" to the keyring
    err := keyring.Set("your-app-name", "johndoe", "hunter2")
    if err != nil {
        log.Fatal(err)
    }

    // Retrieve secret from the keyring
    password, err := keyring.Get("your-app-name", "johndoe")
    if err != nil {
        log.Fatal(err)
    }

    log.Println(secret)
}

This library is perfect if you need to deal with authentication, as you can allow your users to store these in the OS’ keystore, which brings us to the next library: gopass.

gopass

howeyc/gopass is a very lightweight library that allows you to read passwords from the terminal without exposing the characters written.

package main

import (
    "fmt"

    "github.com/howeyc/gopass"
)

func main() {
	fmt.Printf("Enter your password: ")
	password, err := gopass.GetPasswd()
}

Using this in addition to the aforementioned library works like a charm.

g8

Tired of constantly re-implementing a security layer for each of applications?

Me too, that’s why I made TwiN/g8, pronounced Gate, is a simple Go library for protecting HTTP handlers with tokens.

gate := g8.NewGate(g8.NewAuthorizationService().WithToken("mytoken"))
router := http.NewServeMux()
router.Handle("/unprotected", yourHandler)
router.Handle("/protected", gate.Protect(yourHandler))
http.ListenAndServe(":8080", router)

viper

spf13/viper is a library used for managing a configuration file.

While this is very useful for larger applications that have a lot of configurations, this may not be ideal for smaller applications as it has quite a list of dependencies that you won’t need.

Don’t get me wrong, I think viper simplifies the whole process of managing a configuration file, it’s just that if your applications only has one property it its configuration, there’s no need to add an entire library for it.

go-choice

TwiN/go-choice is a shameless plug is a simple and lightweight library for interactively selecting an option.

package main

import (
    "github.com/TwiN/go-choice"
)

func main() {
	choice, err := gochoice.Pick(
		"What do you want to do?",
		[]string{
			"Connect to the production environment",
			"Connect to the test environment",
			"Update",
		})
	if err != nil {
		println("You didn't select anything!")
	} else {
		println("You have selected: " + choice)
	}
}

go-choice in action

I find myself working on command line tools that require picking between multiple options quite often, which is why I ended up making one project for all of them.