A few months ago, I started my journey from PHP to Go on a new app in my company.
Some 3 months after, I posted another Article on DEV talking about my feedbacks on the language.
Now it's the time to share about the libraries & tools I've been using so far. Note that I don't use any framework nor any ORM. Just the right libraries to do the job.
Libraries
1. Viper
๐ 7,353
Viper v2 feedback
Viper is heading towards v2 and we would love to hear what you would like to see in it. Share your thoughts here: https://forms.gle/R6faU74qPRPAzchZ9
Thank you!
Go configuration with fangs!
Many Go projects are built using Viper including:
- Hugo
- EMC RexRay
- Imgurโs Incus
- Nanobox/Nanopack
- Docker Notary
- BloomApi
- doctl
- Clairctl
- Mercure
- Meshery
- Bearer
- Coder
- Vitess
Install
go get github.com/spf13/viper
Note: Viper uses Go Modules to manage dependencies.
What is Viper?
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports:
- setting defaults
- reading from JSON, TOML, YAML, HCL, envfile and Java properties config files
- live watching and re-reading of config files (optional)
- reading from environment variables
- reading from remote config systems (etcd or Consul), and watching changes
- reading from command line flags
- readingโฆ
2. Gin Gonic
๐ 23,913
gin-gonic / gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Gin Web Framework
Gin is a web framework written in Go. It features a martini-like API with performance that is up to 40 times faster thanks to httprouter If you need performance and good productivity, you will love Gin.
Gin's key features are:
- Zero allocation router
- Speed
- Middleware support
- Crash-free
- JSON validation
- Route grouping
- Error management
- Built-in rendering
- Extensible
Getting started
Prerequisites
Gin requires Go version 1.21 or above.
Getting Gin
With Go's module support, go [build|run|test]
automatically fetches the necessary dependencies when you add the import in your code:
import "github.com/gin-gonic/gin"
Alternatively, use go get
:
go get -u github.com/gin-gonic/gin
Running Gin
A basic example:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK,
โฆ3. Mgo
๐ 1,385
globalsign / mgo
The MongoDB driver for Go
The MongoDB driver for Go
This fork has had a few improvements by ourselves as well as several PR's merged from the original mgo repo that are currently awaiting review Changes are mostly geared towards performance improvements and bug fixes, though a few new features have been added.
Further PR's (with tests) are welcome, but please maintain backwards compatibility.
Detailed documentation of the API is available at GoDoc.
A sub-package that implements the BSON specification is also included, and may be used independently of the driver.
Supported Versions
mgo
is known to work well on (and has integration tests against) MongoDB v3.0, 3.2, 3.4 and 3.6.
MongoDB 4.0 is currently experimental - we would happily accept PRs to help improve support!
Changes
4. Zap
๐ 5,901
โก zap
Installation
go get -u go.uber.org/zap
Note that zap only supports the two most recent minor versions of Go.
Quick Start
In contexts where performance is nice, but not critical, use the
SugaredLogger
. It's 4-10x faster than other structured logging
packages and includes both structured and printf
-style APIs.
logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
sugar.Infow("failed to fetch URL",
// Structured context as loosely typed key-value pairs.
"url", url,
"attempt", 3,
"backoff", time.Second,
)
sugar.Infof("Failed to fetch URL: %s", url)
When performance and type safety are critical, use the Logger
. It's even
faster than the SugaredLogger
and allocates far less, but it onlyโฆ
5. Go.uuid gofrs/uuid
๐ 394
UUID
Package uuid provides a pure Go implementation of Universally Unique Identifiers (UUID) variant as defined in RFC-9562. This package supports both the creation and parsing of UUIDs in different formats.
This package supports the following UUID versions:
- Version 1, based on timestamp and MAC address
- Version 3, based on MD5 hashing of a named value
- Version 4, based on random numbers
- Version 5, based on SHA-1 hashing of a named value
- Version 6, a k-sortable id based on timestamp, and field-compatible with v1
- Version 7, a k-sortable id based on timestamp
Project History
This project was originally forked from the github.com/satori/go.uuid repository after it appeared to be no longer maintained, while exhibiting critical flaws. We have decided to take over this project to ensure it receives regular maintenance for the benefit of the larger Go community.
We'd like to thank Maxim Bublis for his hard work on theโฆ
6. JWT-Go
๐ 4,849
dgrijalva / jwt-go
ARCHIVE - Golang implementation of JSON Web Tokens (JWT). This project is now maintained at:
THIS REPOSITORY IS NO LONGER MAINTANED
The new repository can be found at: https://github.com/golang-jwt/jwt
For more information, see issue #462.
jwt-go
A go (or 'golang' for search engine friendliness) implementation of JSON Web Tokens
NEW VERSION COMING: There have been a lot of improvements suggested since the version 3.0.0 released in 2016. I'm working now on cutting two different releases: 3.2.0 will contain any non-breaking changes or enhancements. 4.0.0 will follow shortly which will include breaking changes. See the 4.0.0 milestone to get an idea of what's coming. If you have other ideas, or would like to participate in 4.0.0, now's the time. If you depend on this library and don't want to be interrupted, I recommend you use your dependency mangement tool to pin to version 3.
SECURITY NOTICE: Some older versions of Go have a security issue in the cryotp/elliptic. Recommendation is to upgrade to at leastโฆ
7. Testify
๐ 6,634
stretchr / testify
A toolkit with common assertions and mocks that plays nicely with the standard library
Testify - Thou Shalt Write Tests
โน๏ธ We are working on testify v2 and would love to hear what you'd like to see in it, have your say here: https://cutt.ly/testify
Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
Features include:
Get started:
- Install testify with one line of code, or update it with another
- For an introduction to writing test code in Go, see https://go.dev/doc/code#Testing
- Check out the API Documentation https://pkg.go.dev/github.com/stretchr/testify
- Use testifylint (via golanci-lint) to avoid common mistakes
- A little about Test-Driven Development (TDD)
assert
package
The assert
package provides some helpful methods that allow you to write better test code in Go.
- Prints friendly, easy to read failure descriptions
- Allows for very readable code
- Optionally annotate each assertion with a message
See it in action:
package
โฆ8. Go-yaml
๐ 2,692
YAML support for the Go language
Introduction
The yaml package enables Go programs to comfortably encode and decode YAML values. It was developed within Canonical as part of the juju project, and is based on a pure Go port of the well-known libyaml C library to parse and generate YAML data quickly and reliably.
Compatibility
The yaml package supports most of YAML 1.2, but preserves some behavior from 1.1 for backwards compatibility.
Specifically, as of v3 of the yaml package:
- YAML 1.1 bools (yes/no, on/off) are supported as long as they are being decoded into a typed bool value. Otherwise they behave as a string. Booleans in YAML 1.2 are true/false only.
- Octals encode and decode as 0777 per YAML 1.1, rather than 0o777 as specified in YAML 1.2, because most parsers still use the old format Octals in the 0o777 format are supported though, so new filesโฆ
Tools
1. Gomock + mockgen
๐ 2,023
Gomock is a testing library that allows you to mock your dependencies and to make assertions on them. Mockgen is a CLI tool packaged with gomock to create your mocks.
gomock
Update, June 2023: This repo and tool are no longer maintained Please see go.uber.org/mock for a maintained fork instead.
gomock is a mocking framework for the Go programming language. It
integrates well with Go's built-in testing
package, but can be used in other
contexts too.
Installation
Once you have installed Go, install the mockgen
tool.
Note: If you have not done so already be sure to add $GOPATH/bin
to your
PATH
.
To get the latest released version use:
Go version < 1.16
GO111MODULE=on go get github.com/golang/mock/mockgen@v1.6.0
Go 1.16+
go install github.com/golang/mock/mockgen@v1.6.0
If you use mockgen
in your CI pipeline, it may be more appropriate to fixate
on a specific mockgen version. You should try to keep the library in sync with
the version of mockgen used to generate your mocks.
Running mockgen
mockgen
has two modes of operation: source and reflect.
Source mode
Sourceโฆ
2. Modd
๐ 977
A file watcher allowing you to restart your app, to run your unit tests and much more
Modd is a developer tool that triggers commands and manages daemons in response to filesystem changes.
If you use modd, you should also look at devd, a compact HTTP daemon for developers Devd integrates with modd, allowing you to trigger in-browser livereload with modd.
The repo contains a set of example modd.conf files that you can look at for a quick idea of what modd can do:
Example | Description |
---|---|
frontend.conf | A front-end project with React + Browserify + Babel. Modd and devd replace many functions of Gulp/Grunt. |
go.conf | Live unit tests for Go. |
python.conf | Python + Redis, with devd managing livereload. |
Install
Modd is a single binary with no external dependencies, released for OSX Windows, Linux, FreeBSD, NetBSD and OpenBSD. Go to the releases page, download the package for your OS, and copy the binary to somewhere on your PATH.
Alternatively, with Go 1.17+ installed, you can installโฆ
I hope that you'll find some of these libs/tools useful for your needs.
Don't hesitate to propose the ones you find great.
Thank you for reading!
Top comments (18)
For anyone else reading this: do not use the
satori/uuid
package.It has a number of very concerning flaws, with this one being the worst:
Please instead use: github.com/gofrs/uuid
Now on to the topic:
You want to build a Frameworkless App, but you use Gin, which is a framework. This is quite the incompatible premise, would you not say?
I would encourage using
gorilla/mux
for small niceties on top of the standard library, I personally think it's enough.Gin introduces too much magic and special "Gin only" behaviour and I personally am not a fan of that.
I though exactly the same ;)
Thanks for the security alert :)
Thanks for the security alert on using satori/go.uuid.
Regarding gin-gonic, I don't use it as a framework, but just as a router.
I'd get rid of gin, go really doesn't need frameworks.
There are many people who think you need a framework as soon as you start a new language, but forget that idea with go.
If you're using it just for the router, you could use julien schmidt directly.
I would suggest my own, but I don't think it's ready yet. I need to make sure it's bombproof first.
PS. I also switched from PHP, never looked back.
Happy Coding
I switched from gin router to httprouter. It's enough for my needs.
I also dropped viper to use builtin env var funcs.
I also dropped zap to use zerolog which is more simple.
In my toolbox, I gave a chance to gorm which a little ORM for Go.
I have to publish a new edition of my Go toolbox.
Nice, yes, simplicity is key for me.
I really like it too! However I've been using
gorilla/mux
on prod for a long time now and it's never been an issue, I really appreciate it's "battle-hardened" quality and feel.It's one of the rare libraries I feel fully confident with because it's proved itself time and time again.
Not saying this isn't true of
chi
, just that, that wasn't the choice we made for production back then, so I can't compare it at that level yet.Thanks for sharing these packages ๐ฆ Iโve been making a few side projects with Go but always spent too much time trying to find the โrightโ libraries to use.
My twin brother started using an open-source project called the SaaS Startup Kit . It is an open-source project of Go code to bootstrap your software-as-a-service startup.
gitlab.com/geeks-accelerator/oss/s...
The goal of this project is to provide a proven approach for building new SaaS that reduces the repetitive tasks in getting a new software service launched to production that can easily be scaled and ready to onboard enterprise clients.
Great post! Do you have a recommendation on handling dependencies in your Go projects? This has probably been the biggest source of confusion in my experience with Go.
Hi Esteban!
I gave a chance to
go mod
even if the support is still experimental in the 1.11, because it should be stable in the release 1.12.I bet it will be the future of Go dependency management and will replace
go dep
.I found out that
go mod
is on the good way to be a solid dependency manager for Go. You just have to init yourgo.mod
by typinggo mod init
and then, every time you'll typego get something
, it will add it to your project's dependencies.It's also a good way to organize your code in modules.
This is great stuff. Thanks for sharing! Just starting out with Go myself.
+1 for chi
I created an authentication package here : github.com/dimensi0n/persona (Cookies way only)
At a first glance it seems very similar to echo which I use for personal projects. I'll try to check how they differ, but if you know, please fill me in.