Tit Petric's Blog, page 4
August 6, 2018
Basic monitoring of Go apps with the runtime package
You might be wondering - especially if you’re just beginning to work with Go, how you might add monitoring to your microservice application. As people with some sort of track record will tell you - monitoring is hard. And what I’m telling you - at least basic monitoring doesn’t need to be. You don’t need to set up a Prometheus cluster to get reporting for your simple applications, in fact, you might not even need external services to add a simple printout of your apps...
July 24, 2018
Writing Great Go Code
After writing two books on the subject (API Foundations in Go and 12 Factor Applications with Docker and Go) and years of writing various Go microservices, I do want to put some thoughts down as to what it takes to write great Go code.
But first of all, let me explain this very plainly to all you who are reading this. Writing great code is subjective. You might have completely different ideas as to what is considered great code, and we might only agree on some points. On the other hand, neit...
June 19, 2018
Listing interfaces with Go AST for gomock/moq
Testing is an important part of software development. Specifically, mocking is a common approach where the developer implements an interface that “mocks” the behaviour of a concrete implementation. Depending on the complexity of the mocked objects, there are many ways you can approach this problem.
Notably, if you have a simple program, your mocks can be simple as well. Let’s assume that you have an interface which implements a key-value store API. Actually implementing tha...
May 8, 2018
Protecting API access with JWT
A common use case for APIs is to provide authentication middleware, which will let a client make authorized requests to your APIs. Generally, your client performs some sort of authentication, and a session token is issued. Recently, JWT (JSON Web Tokens) are a popular method of providing a session token with an expire time, which doesn’t require some sort of storage to perform validation.
This is a continuation of a previous article. If you’re new, you should read Handling HTTP r...
March 12, 2018
Handling HTTP requests with go-chi
RESTful API principles dictate the way applications send and retrieve data from API services. We will be implementing our RESTful API service using HTTP as the transportation mechanism. We will take a look at how to provide this with the Go standard library, and then review the usage with an update to go-chi/chi.
Setting up a simple web serverScalability is usually a thing which is achieved by using a specialized load balancer or reverse proxy. Commonly deployed load balancers might be ngin...
March 8, 2018
An argument for value receiver constructors
When it comes to object-oriented programming, there’s so much prior work done before Go, that a lot of newcomers to Go can’t help but to bring some concepts over. One of these concepts are object constructors, for which Go doesn’t have an equivalent.
Why constructors?There are objects in Go that have to be initialized, for example channels and slices immediately come to mind. You’ll have to call make to set that up.
���The make built-in function allocates and initia...
February 28, 2018
Interfaces in Go
Interfaces in Go are a powerful language construct, which allow you to define and use objects of different types under a common umbrella interface. What does that even mean? Imagine a Car, or a Motorbike. Both have a license plate. When we declare an interface with a License() string function, both Car and Motorbike objects will satisfy this interface.
package main import ( "fmt" ) type Car struct { license string } func (c *Car) Name() string { return "car" } func (c *Ca...February 7, 2018
SQL as an API
If you haven’t been living under a rock, you’ll know that recently there is an uptake in popularity of “Functions as a service”. In the open source community, the OpenFaaS project from Alex Ellis has received good traction, and recently Amazon Lambda announced support for Go. These systems allow you to scale with demand, and execute your CLI programs via API.
Motivation behind Lambda/FaaSLet’s call this for what it is - the whole “serverless” movemen...
January 25, 2018
Go tips and tricks: almost everything about imports
Importing packages is an integral part of most programming languages today, and Go is no exception. Importing packages in Go is, for the most part, a simple process. For most people, the basics of importing is enough, however there are some pitfalls that can show up and it’s always good to be aware of them.
Lets quickly go over the basics. Say you want to output something to the terminal. The fmt package takes care of this. So lets go ahead and import it.
import "fmt" func ma...December 21, 2017
Introduction to Reflection
Reflection is the capability of a programming language to inspect data structures during runtime. When it comes to Go, this means that reflection can be used to traverse over public struct fields, retrieve information about tags of individual fields, and possibly other slightly more dangerous things.
You might know, that several packages in the Go standard library use reflection for their purposes. The example being cited most often is the implementation for encoding/json, which is commonly...


