Day Zero Golang Fundamentals

·

4 min read

Hey there👋, geeks!!! I hope this article found you really well. Before you start reading this I recommend you to google what Golang is and go through its wiki for some historical context.

We will cover three fundamental topics in this Golang tutorial:

  1. Variable declarations and initialization.
  2. Constant declarations.
  3. Printing.

Let's Go...Golang...

Variable declarations and initialization:

Variable declarations:

There are two ways of declaring variables that I've come across.

a. Declare them in one go:

var (
    var_name1     string
    var_name2     int
    var_name3     string
)

Note the absence of commas here, if you come from a JS background like me you may have the urge to append commas every now and then.

Which could be also written as:

var (
    var_name1, var_name3     string
    var_name2                int 
)

b. Declare one by one:

var first    string
var sec      int
var third    string

You must know that golang is a statically typed language so we have to declare(*except when we initialize the variable while declaring) the type of variable while it's declaration.

Variable initializations:

Now that we know ho decare a variable we can look ahead and initialize the variable while declaring like in any other language.

var (
    var_name1     string   = "some_string"
    var_name2     int        = 10
    var_name3     string   = "some_string"
)

OR

var (
    var_name1, var_name3     string = "some_string", "some_string"
    var_name2                int = 10
)

OR we can completely omit the type declaration as,

var (
    var_name1, var_name3, var_name2 = "some_string", "some_string", 10
)

But wait, does this not look like too much boilerplate just to declare variables, where are we ice-age? No!

Golang has a special provision for declaring variables inside functions all you need to do is:

func main() {
     name, location := "Prince Oberyn", "Dorne" 
}

Note the := short assignment statement which can be used in place of a var declaration without any manual type declaration.

However, := can only be used inside functions. Outside a function, every construct begins with a keyword (var, func and so on), and the := construct is not available.

Hush!!! What a savior.

As a special note remember that functions in Go are first class citizens, which allows us to assign a function to variables, and much more.

For Example:

func main() {
    var_name := func() { //a variable that contains a function
        //doing something
    }
    var_name()
}

Constant declarations:

Constant declarations are similar to variable declarations, except that the const keyword is used.

However, Constants can only be character, string, boolean, or numeric values and cannot be declared using the := syntax.

const Pi = 3.14
const (
        StatusOK                   = 200
        StatusCreated              = 201
        StatusAccepted             = 202
        StatusNonAuthoritativeInfo = 203
        StatusNoContent            = 204
        StatusResetContent         = 205
        StatusPartialContent       = 206
)

// This example was copied from educative.io which is an amazing interactive learning platform that I came across recently and have been leveraging.

As a side note remember that inside functions:

const name = "Sahil" //constant declartion + implicit typing 

age := 20 // variable declaration + implicit typing

Let's Print:

The Print and Println methods from the built-in fmt(pronounced as fumt) package are used to print the value of a variable or a constant.

package main

import "fmt"

func main() {
    fmt.Println("Hello, 世界")
}

Note: Println prints the passed in variables/constants values and appends a newline.

An example for better understanding:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, 世界")  //Printf
    fmt.Println("Hello, 世界"). //Println
    fmt.Printf("Hello, 世界")  //Printf
}

o/p:

Hello, 世界Hello, 世界
Hello, 世界

Here, You might've noticed the significance of Printf. But it's essentially used when we want to print variables within the string, like in the example below.

package main

import "fmt"

func main() {
    career := "Software Developer"
    year := 2021
    fmt.Printf("Your life as a %s will change in the year %d", career, year)
}

o/p:

Your life as a Software Developer will change in the year 2021.

Does this syntax look familiar to you, if yes you might be here from C/C++ background which is super excellent.

Fun fact: Developers say that Google's Go language is the C for the twenty-first century when it comes to syntax.

That's it for this article folks, I suggest you play around with the code examples on the officially builtin Golang code playground here.

Thank you, for reading till the end. If you like the article pass it on to the next geek you know. If you hate it, help me improve my commenting.

Finally to serve justice to my friend Hamza Hussein who read the draft of this article and helped me make it more readable read his blog here.