foobar.fun

Just another Web Engineer

Getting Started With Golang (1)

Gopher

The Go gopher was designed by Renee French

Something I love with Golang was, it’s statically typed. Although there is no official IDE or Open Source IDE for Golang, but vim-go was enough for me. Why do I’m learning Golang instead another programming language?

  1. Golang is statically-typed
  2. Golang has great ecosystem
  3. Golang is maintained by someone you trust
  4. Golang has similar API with another language
  5. Golang is Open Source
  6. Golang logo is cute

That’s my $0.02 why I choose Golang. Let’s dive in.

Installation

I use Mac, the easiest way to install Golang binary is to install .pkg. Then, next next next. You can verify are golang succesfully installed in your machine by execute this command:

$ go version

It should display your current golang version. Let’s make some “hello world” project. Create new file, let’s say hello.go, then write this code:

package hello

import (
  "fmt"
)

func main () {
  fmt.Println("Hello World!")
}

Then, run it by executing go run hello.go, it should display “Hello World!”. If you want to build your code to binary, you can execute go build. Then, Golang will create binary code you can execute with your current directory file name. If your hello.go stored in hello directory, then you can execute your binary like this: ./hello. It should has same output like before.

Okay, let’s talk about code above.

package

Every code in golang should be have package name, placed in first line of code. It’s used to identify your code. All of your code in the same directory, should use same package name.

import

It’s used for using another library/package to your code.

fmt

You use fmt package to display “Hello World” to console. fmt is a package to implement formatted I/O. It’s like printf and scanf in C.

In this package, you use Println, Scanln, etc. You can see full of fmt API here

Println

To display output to console, with space & new line. So, if you write this fmt.Prinln("Hello", "World", "!"), the output is: “Hello World !”, with new line appended.

Comment

Comment are good for self-documenting our code. Like others language, comment in golang has 2 kind: One-line & Multiple-line. You can create one-line comment with double slash:

// this is comment

And:

/*
 * This is for multiple line
 * Familiar, right?
*/

Declaring variable & constant

Like other programming language, you can use var to declaring variable, and const for constant. To declaring variable & constant, you have 3 ways:

var variableName variableType. e.g: var myName string

var variableName, anotherVariableName variableType. e.g: var myName, yourName string, multiple variable with same type

variableName := value. e.g: myName := "Fariz" (declaring & initializing variable without explicit type)

absolutely you can initialize variable without explicitly declaring it’s type, like var myName = "Fariz" or myName := "Fariz", but if you only declaring variable wihout initialize it directly, you should declare it’s type (like: var myName string).

You can do the same way for constant.

In the next post we will talk about Types in Go, stay tune.