It’s quite common to use the log package within your code to keep track of things which the end user might not need to see, like deprecated notices and warnings. These are great when they are in production and you are monitoring the logs - but they will show up during your tests. Below is an example test for our example application (even further below) which just asserts that the result of the function is 5.
Programs often need to notify us of events and using services like Slack, Hipchat (or even email) make this convenient for us. This code snippet is a way of sending a message to Slack via an Incoming Webhook - you can set these up in the Slack Apps area. All you need is to set the incoming webhook url and change the text to be anything you want. It does include a timeout (of 10 seconds) if something happens to Slack response times for any reason.
This post isn’t about the benefits or why you should be using https, instead it’s just about how to setup and use it with a basic Go web server. Compared with a basic http server there are two main differences. Firstly we need to generate some certificates and secondly we need to change our code to use the certificates and communicate over a TLS connection. Step 1: Commands to make self-signed certificates
One of the (many) positives of Go is it’s simple but powerful use of concurrency. By using keywords like go we’re able to run functions in parallel. As easy as this is, we often need a way to run our next bit of code once all these goroutines have finished. That’s where a WaitGroup comes in. A WaitGroup allows you to specify how many goroutines have been created then wait for them to all be done.
We almost touched on this in our post about unix time - but in this post we look at how to take an arbitrary date in string format and convert it into a meaningful date in the format we want. This uses two important functions, Parse and Format within the time package. The parse function is interesting because, unlike some programming languages, to parse a date you don’t specify the date using letters (Y-m-d for example).
Within Go, like other languages, we have the ability to call external binaries. These allow us to do all sorts of things, but in our example we’re just going to print out our go version by calling our copy (located in /usr/local/go/bin on my computer). The Command function within the os/exec package will allow us to do this. It accepts at least one string - the name of the command/binary you’re trying to run - followed by any number of strings for it’s parameters.
This is an issue in programming when numbers are stored as strings - because as strings, when sorted alphabetically they will go by each digit, from first to last. You might encounter this problem when dealing with numbered file names, for example, which will be treated as strings but we might need to process them in order. In our example below we use both the sort and strconv packages to sort the array and convert the strings to numbers within our comparison function.
We programmers often use temporary files and this example shows how we can create and write to one. We used the ioutil package which has functions for just this. The TempFile() accepts a folder and a prefix. As the folder we get the os’ temp directory (which would be /tmp on unix systems) and for the prefix we use a string, but if we don’t want one, we just pass an empty string.
The example below shows to how to encode and then subsequently decode a string using base 64. Doing this has many uses, one of which to safely encode byte data in structures like JSON. We use the encoding/base64 package to do this, which takes in and returns a byte array into it’s EncodeToString and DecodeString methods. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package main import ( "encoding/base64" "fmt" ) func main() { myString := "This is golangcode.
We’ve already covered how to download a file, but this post covers it a little further by specifying the maximum time attempted on the request. Adding timeouts are often important to stop your program coming to a halt. To do this we make our own http client, giving it our custom timeout value. We can then use this client to make our requests. If a timeout is reached, the Get() function will return with an error.