Gopher Coding Logo

Gopher Coding

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

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.

#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.

#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

How to Write Data to CSV in Go

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.

#csv#write#encoding#flush#struct#slice#users#people#file

Top Golang Books for Learning in 2023

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 syntax to developers with experience in other programming languages, tutorials aren’t enough.

#books#learning#golang#author#2023#learn

Deploy a Go App to Railway

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.

#railway#heroku#hosting#app#repo#project#echo#variable#setup

Add Swagger Docs to a Go API

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.

#swagger#documentation#generate#cmd#tool#install#middleware#swag

Get/Set HTTP Headers in Go Request

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.

#http#headers#set#get#net#request#handler#router

Print Current Memory Usage

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.

#memory#stats#runtime#package#stdlib#memstats#alloc#garbage#gc#os
page -= 1 page += 1 page := 2
Privacy Policy
Site illustrations (of Goldie the Bird) drawn by Carina Roberts.
Tutorials & Site Content © 2023 Gopher Coding
Code Licenced under MIT Licence
Gopher Coding Logo