This is a matching post to “Writing to a File” and explains how to simply get the contents of a file as text and print it to screen. There are different ways to achieve this in Go - all valid. In this guide though we've gone for the simple approach. Using ioutil makes this easy for us by not having to worry about closing files or using buffers. At the cost though of not having flexibility over which parts of the file we need.
Some applications and programs can be very time sensitive - and they often need to return something in a timely fashion. However, it's not always within our control to set a cut off point these operations. Go makes this process somewhat easier though through it's use of goroutines and channels. In the example below we execute LongRunningProcess which we've given 3 seconds complete - but it contains code to sleep for 5, so it will never complete.
This is part 1 of how to find the dominant colours within an image. You might spot this functionality around the internet, as it's used to give images background colours before the image has loaded. We use a package to do this - which manages much of the heavy lifting. The package, called prominentcolor, uses the Kmeans++ algorithm to work this out. By default, it will return us the three most popular colours after both cropping and resizing the image.
In this post we look at how to check if a given string starts with something. This is often used in programming and there are different ways to achieve the same goal. We provide two options in this example. The first is to use the strings package along with the HasPrefix function - this is probably the simplest/best solution. We did include a second option though, if you know how long the prefix that you're looking for is, you can use the string as a slice and check it's value.
This is an overview post (pun intended) about how to work with post request data sent to an endpoint via HTTP. This is slightly different to data stored in query parameters sent in the url and has to be handled differently. Because we're using a http server, we can parse the request (in variable r) using ParseForm() and then use the data from the map generated. This will only work with data sent with a Content-Type header of application/x-www-form-urlencode or multipart/form-data.
Go has a simple built in way to download packages. If you've programmed in Go, you've likely used the go get command before (view docs). It often works something like this: go get -u github.com/gorilla/mux Which is great - but! What if you want to download all the packages linked to your project? This commonly happens if you pull a new project down using version control and don't want to go get each package individually.
Sublime Text 3 can be a great editor for writing Go code - and with a few simple tweaks it can get better. One example of a simple workflow improvement to automate the running of go fmt each time you save. This keeps your code neat and tidy, while also alerting you to any syntax errors. Some things you'll need to get started: Go installed (with a GOPATH) Sublime Text Installed 1) Install Package Control Firstly, we'll install Sublime's Package Control by going to their site: packagecontrol.
Is JSON for computers or humans to read? Well, hopefully with this snippet it can be for both. Instead of just dumping all the json in one line, we instead format it and indent it for readability purposes. Using the MarshalIndent function in the json package we're able to not only specify the data to encode, but also a prefix and an indentation string. In our example we're not to worried about the prefix, but the indentation (3rd parameter) allows us to structure our code.
There are many guides on how to setup a docker container running Go, but the aim of this post is to provide a basic starting point - as often they become complicated and split across many files. It's also aimed at getting it running locally for development purposes quickly, so it might not be production ready. To begin with we create a basic http server, which just says status: ok on the root endpoint.
In Go, we can store and use dates using the time package and although a date in Go cannot be saved as null (because there's no such thing) there is an unset state. This unset state can be shown as 0001-01-01 00:00:00 +0000 UTC and there's a simple way we can check if a date variable has been populated, as demonstrated below. It's also important to note that these are not unix timestamps, which go back as far as 1970, but can handle a large spectrum of dates.