Check If a Date is in The Future?
Whether you’re setting up a calendar event or issuing a JWT token, knowing how to manipulate and validate time can save you from many potential headaches. In Go, the built-in time package provides a comprehensive set of tools to work with dates and times. View Time Docs How to If you check the date in question, is after the current time then this basically does a ‘am I in the future’ check.
Generating UUIDs in Go
UUIDs (Universally Unique Identifiers) serve as a standard for generating identifiers that are unique across time and space. In this blog post, we’ll explore how to generate UUIDs in Go using the google/uuid package. These can be especially useful when generating IDs across different systems - where it wouldn’t be possible to track using an auto incrementing integer.
Should I Commit the go.sum File to the Repo?
TL;DR Yes, probably. Using Go Modules, you will have both a go.mod and a go.sum file within your coding repository. A question often asked is whether you should commit the sum portion of the file as it’s automatically generated. These files help manage the dependencies of your project and they differ by the go.mod file being human-friendly, listing the libraries used within the project, and the go.sum listing the very specific vendor versions of each dependency (not just your direct ones, but also the whole tree).
Find the Min or Max Value in a Slice (New to Go 1.21)
Recover from a Panic
How to catch a panic error when it’s thrown? That’s what this post hopes to answer. Go has a built in recover() function which allows you to pick up and run some code when a panic is thrown. This can be useful for regaining execution of your program, or allowing the panic to happen, but to clean up state (like files) before your program closes. If you are curious what the structure of a panic is, see it’s docs here.
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.