How to Ignore Weekends in Go
Given a date, we’re looking at how you can add a duration to it, like a month or two, but ignoring weekends. This is usually most used for businesses focused on working-days - but it could easily be adapted to do the opposite, only weekends. It’s built around the standard lib time package so no extra packages needed. We’ve got some example code below that makes a date and adds on 14 days, while skipping the weekend days out.
Add Date Ordinals (UK Date Format) in Go
Go, despite its robust standard library, does not support ordinal dates out of the box like ‘th’ and ‘st’. This makes producing dates in the format “1st January 2000” much harder - which we use a lot here in the UK. In this post we’ll aim to show how you can make this easier. For reference, these are the options Go uses when chosing a new format: 1 Mon Jan 2 15:04:05 MST 2006 Our example below implements this for ourselves, as we create formatDateWithOrdinal() to print a given time in this format.
Redis: Connect, Set & Get in Go
Redis is a in-memory data store, used a lot in the backend systems of web-apps. Specifically, it’s good at helping with caching and message as it’s high performance lends itself to fast response times at retriving data. In this blog post, we will demonstrate how to connect to Redis using Go and the go-redis library, which is one of the most popular and widely-used libraries for interacting with Redis in Go.
Search & Replace in Strings
One common task when working with text in code, is searching and replacing inside strings. In this blog post, we’ll explore two ways to perform a search and replace operation in Go using the strings and regexp packages. Strings Package The strings package provides a simple method called Replace for search and replace operations. This method has the following signature: 1 func Replace(s, old, new string, n int) string Here’s an example of how to use the Replace method:
Organizing Structs for Memory Efficiency
Go is generally a very memory efficient language to work in, but knowing this little technique can make it that bit more efficient again. First, we’ll look at structs - they are a composite data type used to group together zero or more values, each with its own name and type, under a single name. They are the foundation for building complex data structures and objects. Memory alignment is an essential aspect to consider when organizing structs in Go.
Reduce Go Binary Size
Always a hot-topic in Go circles is binary size of applications. In this post we will show you how you can reduce the binary size produced from go build for leaner, slimer builds. In short, adding the ldflags shown below will reduce the size of the binary produced. It does this by removing some perhaps non-critial parts (shouldn’t affect execution, only debugging potentially). -s Will turn off the symbol table, so you won’t be able to use commands like go tool nm.
Using Iota for a List of Constants (Like Enums)
Enums have been used in many programming languages in varying forms for many years. But if you’ve found this post, then you are most likely looking for how to do this in Go (golang). In Go, there’s a handy feature called iota which is a running list of ever-increasing constants. They are a consise way of presenting a list of things in a “don’t repeat yourself” way. For example if you need to define days of the week using contants, you could:
Run an SQL Query Through SSH
This post shows you how you can connect to a database remotely via an SSH connection. This is especially useful if the database isn’t accessible due to firewall rules - for example on a web server. But if you have SSH access, it’s just like logging in and running the command yourself. This is possible with port forwarding through an SSH connection on our machine, like shown below, but it’s more consise and simpler being part of a single script.
A Basic Web Scraper in Go
A basic web scraper usually involves a few steps, fetching the content, querying it for the data you’re after and then sometimes, using that data to go and find more as a loop. In our example below, we use a package called goquery to do most of the heavy lifting for us. This library will go further than Go’s standard library by allowing us to do both the first and second steps a bit easier.
How to Format Your Golang Code
Whether you agree with it or not, Go defines a code style at a language level (which I love), and not different styles per project - or having the style as an afterthought! This means there’s very little opinion or conflict when moving between projects so it’s easy. This is very important to Go, as it stresses clarity and consistency as some of the most important factors of code style.