This is just a quick post showing how we can check if a URL is a valid one. URLs are often passed from third parties or user input, so they need to be checked before used. This function allows you to do just that, it will return true if the string is a valid URL. Feel free to copy and paste it and go from there. e.g. valid “https://gophercoding.com/" e.g. invalid “test string”| Home on Gopher Coding
Unix time is the number of seconds since ‘Epoch’ (00:00:00 UTC on 1 January 1970). It’s a widely adopted way of representing time in a simple, uniform, manner - as it’s an integer and not a string. Because of it’s simplicity, it also used it many programming languages. In Go, you can easily access it from the time package by getting the current time, then calling the Unix() function.| Home on Gopher Coding
There a many benefits to testing your code in general, which we won’t go into detail on (but if you’re interested, take a look here) all we will say is the quote below. “Imperfect tests, run frequently, are much better than perfect tests that are never written at all”. - Fowler First Test We’ve created the most basic progam, to add two numbers together, below in main.go which we’re going to write a test for.| Home on Gopher Coding
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.| Home on Gopher Coding
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.| Home on Gopher Coding
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, b...| Home on Gopher Coding
The latest Go release, 1.21, has introduced two new built-in functions: min() and max(). These functions have long been used in many other languages, such as Python and JavaScript, and are now welcome additions to Go. The min() and max() functions have been introduced to simplify the process of finding the smallest and largest numbers in a set, respectively. There was plenty of talk as to whether these should be included as “built-ins” or introduced into the cmp package.| Home on Gopher Coding
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.| Home on Gopher Coding
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.| Home on Gopher Coding
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 t...| Home on Gopher Coding
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.| Home on Gopher Coding
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:| Home on Gopher Coding
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.| Home on Gopher Coding
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.| Home on Gopher Coding
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:| Home on Gopher Coding
This post shows how you can connect to a database remotely via a SSH connection. In our example we’ll be connecting to a MySQL or MariaDB database, but the same method will apply to many other SQL databases like PostgreSQL. This technique 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.| Home on Gopher Coding
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.| Home on Gopher Coding
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.| Home on Gopher Coding
You can use the golang.org/x/crypto/sha3 [docs] package to perform SHA-3 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using Sprintf. Advantages of Using SHA3 Security: SHA-3 was designed to provide higher security compared to SHA-2 by using a different construction called “sponge construction”, which makes it more resistant to various types of attacks, such as collision and...| Home on Gopher Coding
You can use the crypto/md5 [docs] package in Go to perform MD5 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using Sprintf. Note: There are more modern approaches than md5 these days - and it isn’t recommended for many things, but definitely not password hashing. Here’s an example of how to use it:| Home on Gopher Coding
In Go, you can use the standard library package encoding/csv [docs] to write data to a CSV file. Below is a example that shows you how you can write a slice of user-data related strings to a CSV file. The code creates a new file called users.csv and writes a slice of records to it. Finally, the Flush method is used to flush any buffered data to the underlying io.Writer, which is the file.| Home on Gopher Coding
1. Go Programming Language Google’s Go team member Alan A. A. Donovan and Brian Kernighan, co-author of The C Programming Language, provide hundreds of interesting and practical examples of well-written Go code to help programmers learn this flexible, and fast, language. View on Amazon 2. Learning Go: An Idiomatic Approach to Real-World Go Programming Go is rapidly becoming the preferred language for building web services. While there are plenty of tutorials available that teach Go’s synt...| Home on Gopher Coding
After using Heroku for many years, I’ve recently taken a look into Railway as an alternative hosting platform for various side projects. These projects are often written in Go (of course!) so I thought I’d write a quick guide on how to setup a new project - the good news is, it’s easy! 1) Get an Account It’s easy to setup, the link on their home page will help you.| Home on Gopher Coding
Swagger allows developers to easily create api documentation which is defined using comments in the code and then generated as a yaml config file. In this post we’ll be using it alongside Go (golang). Using tools like Swagger has many advantages, like allowing you to generate documentation automatically (saving you time) and that it keeps your code and documentation as close as possible (in distance terms). The idea being that if the docs are hard to update and far away, you just won’t.| Home on Gopher Coding
HTTP headers, we all need ’em 😉 Here’s how you can get and set these headers within your Go requests. These include request coming into your router handlers and requests you are sending out to other systems through net/http. This can be thought of as being the same as reading headers from a request and creating new ones. First we’ll start with reading them from the request.| Home on Gopher Coding
In this post we show how you can print memory usage in golang. We do this by outputting the current state of memory at any given time to show how much ram has been allocated and gc cycles made. We have created a function PrintMemUsage() to help out, so you can call this when ever you need to know. All the info we need can be acquired through the runtime [docs] package, which allows us to read the state of the memory into the MemStats struct.| Home on Gopher Coding
Picking up an old Go project, we wanted to update the desired version it should run on (mainly so when we deployed to our live systems it would use this version too). To do this, we updated the version within the go.mod file - the file which keeps track of both versions and packages used by the project. We used the command below:| Home on Gopher Coding
Having come from the PHP community, we often have a handy debug function at our disposal dd() (part of Laravel) and var_dump() (native). This may not be perfect (compared with a full on debugging suite) but it is a quick and easy debugging method. This post gives an idea on how you can do the same, but in Go. We’re helped out by being able to use both variable parameters (shown with .| Home on Gopher Coding
We won’t go into too much detail about HTTP status codes themselves, but in this post we will talk about how to use the status code after making a request, how to check them as a range and how to print them as text. This is often important so we can check if something was successful or failed. You can always get this data if you have a net/http/Response type (spec).| Home on Gopher Coding
Using Go, we often make HTTP calls these days using net/http, which result in a response of type io.ReadCloser… which are hard to read for the layman (like me). What we really want to the response in the form of a string which we can read. This post will talk about how to convert these ReadCloser into strings. First we’ll look at the problem, then we have two different solutions.| Home on Gopher Coding
We often need to remove symbols and special characters from the strings we’re using (especially with currency!). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in “$1,000” you can turn it into “1000”. We use the regexp package to do this, first building a regex with .Compile() then running the string through that regex with .| Home on Gopher Coding
This post shows how you can download a file in Go from a URL. We use the std lib (standard library) http.Get() [docs] and io.Copy() [docs] functions to help us with this. This function should be efficient as it will stream the data into the file, as opposed to downloading it all into memory, then to file. The file will be saved in the same directory as your program. We also show an alternative below if you want to take the filename from the URL.| Home on Gopher Coding
The “High Efficiency Image File Format” or HEIF is an image format often used by Apple devices. Although called HEIF, the file types are often heic (presumably the ‘c’ stands for container?) They use a similar encoding method of video formats and are deemed better quality and lower file size than standard jpeg files. In our example, we’re trying to convert these heic files back into jpg files though - so we can use them elsewhere, display them or what ever we choose.| Home on Gopher Coding
You can find the length of an array, or to be correct a slice or map, in Go by using the standard library function len(). We use the term array loosely here, as a general variable holding multiple things. Maps tend to have defined keys, whereas slices don’t (more info on the difference here). We have shown both as examples below, how to get the length of a slice and how to get the length of a map.| Home on Gopher Coding
Sleeping in Go and how to pause execution and sleep for a number of seconds in Go (golang). We can use the time package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided. In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this.| Home on Gopher Coding
This is one of those posts that won’t need a huge introduction. We want to convert a string into an integer. We’ll look at the different ways to do this (based on which sort of int you want). We’ll convert to int, uint and uint64 (though a int64 will be easy enough work out).| Home on Gopher Coding
Automation is great (right?!) and it can be so easy these days to run your tests automatically on any new pull request or code change. In this post we outline how you can automatically run your Go (golang) test for your project by using Github Actions. The results will then show within Github and is configured using a yaml file. To get this running, you will need to create a workflow file (as shown below).| Home on Gopher Coding
This is a short and sweet article on running a Go application using Docker. This is a generic example and many use-cases will differ and so have totally different setups. In our examples we’re also using Docker and docker-compose both of which you will need already installed and setup on your machine for this to work.| Home on Gopher Coding
If you want to update all dependencies and packages within your project then the simplest way is to use the go get command which handles it all for us. Go modules (go mod) will handle most of the versioning of these packages, so deploying and sharing the project will keep the same package versions.| Home on Gopher Coding
If you just dabble with Docker, or use it regularly, you will build an arsenal of images and containers as you go along. From time-to-time we need to clear these out. This process used to be more complicated until the docker command implemented a simpler way using system prune.| Home on Gopher Coding
For logging purposes, performance monitoring, debugging - what ever your reason, it can be useful to know if Go has reused it’s connection when making an initial request, for later use. If it’s not reusing connections, it might be running slower and less efficiently than it needs to be. The code below is used as an example of how to log if connections are being reused, as well as outputting the DNS information gathered.| Home on Gopher Coding
Microservices are a form of service-orientated architecture - we won’t go into too much detail on what they are, but we will discuss their pro’s and con’s here. As with all architectures, there is no right way that everyone should use so discussing their plus points with the potential issues is important. Advantages First a quick look at why you should use them. Independent Technology Using microservices to split up your infrastructure allows you to use different technology/programming ...| Home on Gopher Coding
This article describes how to install the latest version of Go (golang). It also helpfully updates itself by pulling the latest version numbers directly, so you don’t have to go and dig out the latest version each time they release a version (yay!) - just copy and paste away. For more info, try their installer docs.| Home on Gopher Coding
Environment variables are used throughout the coding ecosystem as a way of keeping secrets out of the code. They’re also useful as a way of keeping the code the same between environments, e.g. live, uat and test servers but functionality might work differently.| Home on Gopher Coding
Life can be simple when all your Go files are in the root folder. But when a project gets a bit bigger, you may want to store much of the logic in packages. The issue comes when our usual command of go test then no longer picks up our tests.| Home on Gopher Coding
While loops do not actually exist within Go (golang), by name, but the same functionality is achievable with a for loop. Which is great, why have a ‘while’ when the same can be done with ‘for’. Our example below shows how you get a Go program to print out a message every second to the screen. (You’ll have to exit to application to stop it, e.g. Ctrl+C)| Home on Gopher Coding
Hello! This is our first post on GopherCoding.com where we’re going to list snippets of Go (as in Golang) code to make learning and building things easier. A Bit of Background Go is a programming language, heavily backed by Google, and racing up the list of most popular languages to code in. It’s a statically typed, compiled language and is well known for it’s concurrency. Great for web applications, command lines tools and many other things.| Gopher Coding
Netlify, the hosting and web platform, allows you to create "functions" along with their CDN. These functions are hosted on AWS’ Lambda and can be accessible via a URL. So you can create static sites, with extra ability and dynamism (like we use on this site). We wanted to share a post giving an example how to write one of these functions in Go. The aim of the code (below) is to return the version of golang.| Gopher Coding