1. HOME
  2. Blog
  3. Golang
  4. My First Golang

My First Golang 

The strength of Golang is in its parallel processing capabilities. To replace crawlers or stress with Golang, let's write a http request code using parallel processing. First things first, let's start with Hello World!

Getting Started 

I first started with some initial setups for Go environment and essential build settings as a Sublime user.  Having notations such as ': =' and using modern abbreviations, also using methods similar to C for faster speeds. Below is a wiki page as reference.     
https://en.wikipedia.org/wiki/Go_(programming_language)

Here's the "Hello World" I'm going to share this Go Playground so feel free to play around with it. The import and main are very natural and was easy to write.   

Easy!... actually, I didn't realize I was writing Printf instead of printf and I actually got stuck for a bit. I'm already learning about the notation rules for Golang. Below is from the documentation Effective Go

"Names are as important in Go as in any other language. They even have semantic effect: the visibility of a name outside a package is determined by whether its first character is upper case. It's therefore worth spending a little time talking about naming conventions in Go programs."

I Want To Execute a Http Request 

From my experience, the learning process becomes a lot more fun and time efficient when you practice writing code as a web application.  When looking at an Go example http package I found the line defer resp.Body.Close() after acquiring a response.  Explicitly executing the end of an response is not hard to wrap my head around but defer  is something that is not familiar with me and seems to be a notation specific to Golang. Let's take a closer look.  

When writing with this description, the export order is the following 1.Start, 2.End, 3.True End. You can see that  The line that had defer  applied was executed after the initial function.  The convenience of this will make more sense as we further our learning. 
What happens if we use defer a number of times, within a function? Let's take a look. 
Go Playground
We can see that the defer that was written afterwards gets executed first. In Golang, Exceptions are raised by using the term Panic. Even then, lines that have been defer-ed will execute so let's try that.  
Go Playground

Even after the processing terminated due to an error, the defer lines still executed. The lines that did not execute are the lines after panic. 
This is it for the introduction.  We'll be playing around with some print in the next installment!