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.
Sentiment analysis is an interesting technique of judging whether a sentence, word or paragraph is considered a positive or negative thing. It’s often used against datasets like tweets as it allows you to summarize a mass of small sentences into an easy to understand stat. In our example, we’re using a package called sentiment - which was trained against IMDB comment data - to judge whether our example sentences are positive or negative.
The foreach keyword itself does not exist within Go, instead the for loop can be adapted to work in the same manner. The difference however, is by using the range keyword and a for loop together. Like foreach loops in many other languages you have the choice of using the slices’ key or value within the loop. Example 1) In our example, we iterate over a string slice and print out each word.