Loading...

Follow GolangCode on Feedspot

Continue with Google
Continue with Facebook
or

Valid
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.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
This is an example of checking if a pair of long/lat coordinates lie within a polygon or multipolygon when working with geojson. It’s often useful when working with geo-special data and maps to determine if the point your looking is within an area - or which area it’s within. We use the paulmach/orb package (see on github), which is deep and precise library for dealing with all sorts of spacial and geometric data.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
If you’ve ever come across messages like these, you’ll no doubt have used type assertion already. This is a post explain how and why to use it. cannot convert result (type interface {}) to type float64: need type assertion invalid operation: myInt += 5 (mismatched types interface {} and int) Functions and packages will at times return interface{} as a type because the type would be unpredicable or unknown to them.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
Using a web server as an example, there are multiple stages you can load resources. Within the main() function and within the handler are the obvious two - each with their own advantages and disadvantages. Within the main function can hinder the start-up time of the server, while code within the handler is run on every request. Sometimes we want to load a resource only once and when it’s first needed.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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 respose times for any reason.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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.
Read Full Article

Read for later

Articles marked as Favorite are saved for later viewing.
close
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview