Learning Golang — Day 1

Hasan Gürcan
2 min readJan 5, 2021

In the previous Blogpost we prepared the development environment for starting to learn Golang. This time we will focus on our first “Hello World” and Variables.

Let us prepare the folder and files needed for this first application.

In WSL change to your work directory or to the cloned git repository. In Terminal create a new folder that should not be tracked by git (you can backup your files after each day and I will put folders named “*_ex” (ends with ex for exercise as suffix) to the gitignore file for convenience.

mkdir 01_helloworld_ex
cd 01_helloworld_ex
touch helloworld.go
vim helloworld.go # you can use any editor you like

Finally we start coding!

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

You compile and run it with

go run helloworld.go

And you can create a binary for executing it directly with

go build helloworld.go
ls # should print you the binary 'helloworld'
./helloworld # now you can execute it as executable

The go compilation is fast. There are even myths about the origin due to this.

The origin myth for Go states that it was during one of those 45 minute builds that Go was conceived.

Short explanation on what we know so far:
* We can define packages
* We need to import “fmt” to have access to “Println” to print to the standard out
* functions will be declared with “func” and the main function is apparently called “main”

Variables

Visibility of Variables are handled if the variable name is written in uppercase or not. There is no keyword like in Java `public` or `private` but uppercase of the first letter means the variable will be exported. We will see if the same applies for functions too but this would explain why function names are starting with an uppercase, right?

Variable basic types are:

  • bool
  • int8, int16, int32, int64, uint8, uint16, uint32, uint64 and uint
  • float32, float64
  • complex64, complex128
  • byte # this is an alias for uint8, representing characters → for ASCII chars
  • rune # this is an alias for int32 → for UTF-8 chars
  • string

The ranges are based on the number at the end. For example int8 is 8-bit int signed and would mean -128 to 128 whereas the uint8 means unsigned 8-bit int and its range is from 0 to 255.

When initialising variables they have default values

  • bool’s default is false
  • numericals default is 0
  • string default is an empty string

Variables Declaration

//default value will be zero
var num int
//multiple variables assignment in one line
var num1, num2 int = 12, -24
// variables assignment without type declaration with ':=' operator but only in loops and functions
num3, num4 := 7, 14
VisibleNum := 100

We can print the values with and do also our first calculation. String concatenation can be done with `,`.

fmt.Println("Value of defaultnum", defaultnum)fmt.Println("Sum of first two numbers", num1, " + ", num2, " = ", num1 + num2)fmt.Println("Sum of next two numbers", num3, " + ", num4, " = ", num3 + num4)

In the next blogpost we will focus on loops and functions.

--

--