Whether you like it or not, Go defines a code style at a language level (which I love), not different style guides per project. This means there’s very little opinion or conflict - and moving between projects is 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, but I’ve been told to format it?
Or, maybe you’ve been fixing your style manually as you go along and would like to automate it?
Go Format
or go fmt for short.
If you run this in the command line it will format the code of the project that you are located in. This comes with Go, so there’s nothing extra to install.
1
go fmt
-n (optional) will print the command (and list of files) it will run, but does nothing.
-x (optional) prints out the command it will run, then runs it.
Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.
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.
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 definately not password hashing.
Here’s an example of how to use it:
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.
Using tools like Swagger has many advantages, like allowing you to generate these 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.