Underscores in Imports Underscores in Go serve a few purposes. In U+005F, “_” is a letter (as a string). You may also see them as [ “_” ] in between successive digits and a base prefix. In this case, it’s to improve the readability of the code. You may also see them in function names, e.g., imaginary_lit. The underscore is quite a versatile tool. They even have their place in defining the kind of action that will take place in a function.
In the Go world, an underscore (_) before an expression is called a blank identifier. As you may already know, identifiers—user-defined program components, e.g., name of a function, variable, or package—in Go must be preceded by an underscore or a letter (a-z or A-Z). If they aren’t, you’ll receive the compile- time error which essentially means Go cannot read the syntax of your code (you didn’t write your code correctly).
Validating email addresses, like many thing, are one of those things you can put any amount of effort you want into. The solutions range from nice’n’quick, by using regex to check the email address is formatted correctly - through to complicated, by actually trying to interface with the remote server. There are also some middle grounds available in between, like checking the top level domain has a valid MX record and detecting temporary email addresses (if these aren’t wanted).
In this post we will look at leveraging Chrome’s debug protocol to load up a webpage and take a screenshot. This is all made possible from a package called chromedp which allows us to control a Chrome instance through our Go code. You will also need Chrome installed or to be using something akin to the chrome/headless-shell docker image. We’ve split the process in code up into: Start Chrome Run tasks: like loading the webpage and taking a screenshot Saving the screenshot to file
If you are coming from a PHP background you’re probably very used to functions like urlencode() and rawurlencode(). The good news is you can do the same in Go and rather simply too. In the net/url package there’s a QueryEscape function which accepts a string and will return the string with all the special characters encoded so they can be added to a url safely. An example of is converting the ‘+’ character into %2B.
Exiting an application in Go happens anyway when the main() function is complete, but the purpose of this post is to show you how to force that to happen. Perhaps at an earlier stage than the end of your code, perhaps with an error code too. This is a well established method in POSIX systems, whereby a program can return a 0-255 integer to indicate if the program ran successfully, and if not, why not.
Sometimes getting a database connection up and running can be a bit fiddly, we’ve all been there, and it can help to have an example to work from. This post aims to show you the complete basics of creating a database connection, forming a query to run and populating a struct with our resulting data. To do with we use the database/sql interface and load in the pq driver/library to actually do the work for us.
One of the many benefits of using Go is it’s simplicity when it comes to concurrency. With WaitGroups being an excellent example of this. It can be tricky though to handle both concurrency and errors effectively. This post aims to outline how you can run multiple goroutines and handle any errors effectively, without stopping program execution. The essence of this comes down to these three key parts: Creating two channels, for passing errors and when the WaitGroup is complete.
A common scenario a back-end web developer might encounter is writing code which uploads a file to an external storage platform, like S3 or Azure. This is simple enough, but writing tests for this code which are isolated from the dependencies isn’t quite as straight forward. We can achieve this in Go through the use of interfaces and creating a “mock” uploader when our tests run. Below we’ve build an example to show this, first showing the test and then the code it’s testing.
Argon2 is a password hashing algorithm which was voted the winner in the Password Hashing Competition in 2015. It has implementations in many programming languages these days, with Go being no exception, and is often recommended over tools like bcrypt. It is, however, a little difficult to use the library directly in Go and this post is designed to provide a wrapper for the library and provide two functions at the end of it.