It’s been a while I am looking at Go. The language is nice, feels like a mixture of Python and C with huge performance gains and some innovative language features. In this post, I would try to quickly focus on the basics of “Go”. I hope I shall write more on the language in the coming days.
Installation
Installation is really platform specific. I would happily forward you to http://golang.org/doc/install for installation instructions on your OS.
If you happen to have the same OS as mine, that is OSX, I highly recommend installing Go using Homebrew. It’s just a matter of one line –
1 |
brew install go |
Once it’s installed, you can display useful information with this command –
1 |
brew info go |
If you are on Windows, Linux or any other OS, please follow a suitable instruction set from the documentation.
Running Go Programs
How does a first program look on Go? Let’s see –
1 2 3 4 |
package main func main() { print("Hello World! \n") } |
A Go file must define a function named main if you want to run it. We can define a function in go using “func” keyword.
Running it –
1 |
go run main.go |
You can directly run a Go file by using the “go run” command. This comes in handy for debugging, if you want to build a compiled binary, use “go build”.
1 2 3 4 5 6 7 8 9 10 |
Lighthouse: ~/Codes/go $ go build main.go Lighthouse: ~/Codes/go $ ls main main.go Lighthouse: ~/Codes/go $ ./main Hello World! |
Variables, Constants and Printing
First, how do we define variables in Go? It’s simple –
1 |
var variableName variableType = "default value" |
In Go, we can import packages using the “import” keyword. The package “fmt” has a nice function named “Println” which is more or less Go equivalent to System.out.println() in Java. Let’s see a code sample –
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package main import "fmt" func main() { //var variableName variableType = "default value" var name string = "masnun" var age int = 23 fmt.Println(name); fmt.Println(age); } |
If you are initializing a variable, you can optionally skip the variable data type. Go can infer that from the initialization.
There is a shorthand format for declaring and initializing a value quickly –
1 |
name := "masnun" |
It will create a var type string and assign the value “masnun” at the same time.
To create a constant, we just replace “var” with “const”. Example:
1 |
const s string = "constant" |
Looping
In Go, we use “for” for all types of looping. Check out –
While Loop:
1 2 3 4 5 6 7 8 9 10 11 |
package main import "fmt" func main() { i := 1 for i <= 10 { fmt.Println(i) i = i + 1 } } |
Generic For Loop:
1 2 3 4 5 6 7 8 9 |
package main import "fmt" func main() { for i := 1; i <= 10; i++ { fmt.Println(i) } } |
We can do common “foreach” loops combining “for” with “range”. We shall see them in the next section.
Arrays, Slices and Maps
Arrays:
If you are any programmer at all, I probably don’t need to tell you about arrays. Defining arrays in Go is easy.
The syntax is –
1 2 3 4 5 |
var arrayName [size]type // Example: an array of 5 integers var numbers [5]int |
We can reference the indexes just like in any other language. We can even declare and initialize an array on the same line. Take a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package main import "fmt" func main() { var numbers [5]int numbers[1] = 2 fmt.Println(numbers) var initNumbers = [5]int{1, 2, 3, 4, 5} fmt.Println(initNumbers) } |
Slices:
Slices are like arrays but not fixed size. However, the type must remain the same. I mean you can’t add a string type to an integer slice.
We use the make() function to create a slice. In fact, in Go, make() can create a lot of stuff for me. We shall soon find out. For now, let’s see how we can use a Slice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package main import "fmt" func main() { strSlice := make([]string, 1) // we make a slice & initialize with one item strSlice = append(strSlice, "hello") strSlice = append(strSlice, "world") fmt.Println(strSlice) } |
Maps:
Maps are what we call “Hashes”, “Dictionaries” or “Associative Arrays” in misc other languages. Maps are in reality key value pairs. We use make() to create maps of certain types. Example follows –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package main import "fmt" func main() { ages := make(map[string]int) // create a map with string keys and integer values ages["voldemort"] = 50 ages["thedoctor"] = 1200 fmt.Println(ages) } |
Iteration with Range
“range” allows us to iterate over a number of data structures. range gives us the key and value in each iteration. It works quite like the “enumerate” function in some languages (eg. Python). Let’s see how we can iterate over common data structures:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package main import "fmt" func main() { // Arrays nums := [5]int{1, 2, 3, 4, 5} for index, value := range nums { fmt.Println(index, ": ", value) } // Maps emails := make(map[string]string) emails["gmail"] = "masnun [at] gmail.com" emails["work"] = "masnun [at] team.okdo.it" for key, value := range emails { fmt.Println(key, ": ", value) } } |
That’s all for Part – 1. In my next blog post, I shall try to cover “If/Else” with “Structs, Functions & Methods”.