Gopher Coding Logo

Gopher Coding

Go, Docker, DevOps & Distributed Systems
  • Tutorials
  • About

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.

#redis#connection#get#set#goredis#memory#store#package

Search & Replace All in Strings

One common task when working with text in code, is searching and replacing inside strings. For example you might want to replace X with Y. Or equally, you might want to replace Z with nothing, removing it. 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:

#strings#replaceall#search#replace#compile#occurrances#text#change

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. The padding introduced due to alignment can lead to memory waste. To reduce padding and optimize memory usage, follow these guidelines:

#memory#alignment#padding#cpu#hardware#struct#optimization#bytes

Reduce Go Binary Size

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.

#binary#size#reduce#ldflags#symbol#debug#dwarf#stacktrace#production

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:

#iota#constant#enum#language#symbol#enumeration#list

Run a SQL Query Through SSH

Run a SQL Query Through SSH

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.

#ssh#database#db#connection#query#firewall#port#forwarding#agent#pem

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. It uses NewDocument() to get the content, then Find() to query it.

#goquery#scrape#web#spider#goroutine#content

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. So you’ve written some code, and have been told to format it?

#format#code#style#clarity#consistency#language#fmt#command#cli#tool

SHA3 Hash in Golang (256-bit)

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 preimage attacks.

#sha3#hash#hexadecimal#sprintf#fmt#crypto#string#package#security

MD5 Encoding in Golang

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:

#md5#encoding#hexadecimal#encode#hash#sprintf#fmt#crypto#string
page -= 1 page += 1 page := 2
Privacy Policy
Site illustrations (of Goldie the Bird) drawn by Carina Roberts.
Tutorials & Site Content © 2024 Gopher Coding
Code Licenced under MIT Licence
Gopher Coding Logo