Loading...

Follow GolangCode on Feedspot

Continue with Google
Continue with Facebook
or

Valid
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. Becasue we’re using a http server, we can parse the request using ParseForm() and then use the data from the map. This will only work with data sent with a Content-Type header of application/x-www-form-urlencode or multipart/form-data.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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.
Read Full Article
  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
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

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