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.
Go configuration with fangs!
Many Go projects are built using Viper including:
go get github.com/spf13/viper
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 from buffer
- setting explicit values
Viper can be thought of as a registry for all of your applications configuration needs.
When building a modern application, you don’t want to worry about configuration file formats; you want to focus on building…
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 (Golang). 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 Web Framework
- Quick start
- Gin v1. stable
- Build with jsoniter
- Using GET, POST, PUT, PATCH, DELETE and OPTIONS
- Parameters in path
- Querystring parameters
- Multipart/Urlencoded Form
- Another example: query + post form
- Map as querystring or postform parameters
- Upload files
- Grouping routes
- Blank Gin without middleware by default
- Using middleware
- How to write log file
- Custom Log Format
- Controlling Log output coloring
- Model binding and validation
- Custom Validators
- Only Bind Query String
- Bind Query String or Post Data
- Bind Uri
- Bind Header
- Bind HTML checkboxes
- Multipart/Urlencoded binding
- XML, JSON, YAML and ProtoBuf rendering
- Serving static…
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.
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!
Blazing fast, structured, leveled logging in Go.
go get -u go.uber.org/zap
Note that zap only supports the two most recent minor versions of Go.
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
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…
Package uuid provides a pure Go implementation of Universally Unique Identifiers (UUID) variant as defined in RFC-4122. 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 (RFC-4122)
- Version 2, based on timestamp, MAC address and POSIX UID/GID (DCE 1.1)
- Version 3, based on MD5 hashing of a named value (RFC-4122)
- Version 4, based on random numbers (RFC-4122)
- Version 5, based on SHA-1 hashing of a named value (RFC-4122)
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 original iteration of the…
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 1.8.3. See issue #216 for more detail.
SECURITY NOTICE: It's important that you validate the
alg presented is what you expect…
A toolkit with common assertions and mocks that plays nicely with the standard library
Testify - Thou Shalt Write Tests
Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
- Install testify with one line of code, or update it with another
- For an introduction to writing test code in Go, see http://golang.org/doc/code.html#Testing
- Check out the API Documentation http://godoc.org/github.com/stretchr/testify
- To make your testing life easier, check out our other project, gorc
- A little about Test-Driven Development (TDD)
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
YAML support for the Go language
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.
The yaml package supports most of YAML 1.1 and 1.2, including support for anchors, tags, map merging, etc. Multi-document unmarshalling is not yet implemented, and base-60 floats from YAML 1.1 are purposefully not supported since they're a poor design and are gone in YAML 1.2.
Installation and usage
The import path for the package is gopkg.in/yaml.v2.
To install it, run:
go get gopkg.in/yaml.v2
If opened in a browser, the import path itself leads to the API documentation:
The package API for yaml v2 will remain stable as described in…
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 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
Once you have installed Go, install the
To get the latest released version use:
GO111MODULE=on go get email@example.com
If you use
mockgen in your CI pipeline, it may be more appropriate to fixate
on a specific mockgen version.
After installing, you can use
go doc to get documentation:
go doc github.com/golang/mock/gomock
Alternatively, there is an online reference for the package hosted on GoPkgDoc here.
mockgen has two modes of operation: source and reflect
Source mode generates mock interfaces from a source file
It is enabled by using the -source flag. Other flags that
may be useful in this mode are -imports and -aux_files.
mockgen -source=foo.go [other options]
Reflect mode generates mock interfaces by…
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:
|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.|
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.
If you have a working Go installation, you…
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!