Golang tutorial - 1 Introduction
Tags: go, beginners, tutorial
Published: 2020-05-17 03:49:12

#Introduction

Go Programming language also known as Golang is an open-source programming language that was developed by Google to create simple, reliable, and efficient software. There are some reasons why Golang is worth to learn :
* Developed by the experts
* The syntax easy to understand
* Used in web development, especially backend development

## Installing Go in Local Machine / Your Development Environment
The installation guide for Go is available [here](https://go.dev/dl/)

After the installation is finished, you can write a Go program in your favorite text editor such as VSCode, or use a dedicated IDE like Goland. 

Golang playground is also available for learning the Go programming language: https://go.dev/play/

## Writing First Program
To check that Go is installed correctly on your local machine, type this command.

```bash
go version
```
To check the Go environment setup, use this command.

```bash
go env
```

Alright, let's create the file called `main.go` to write the first program. Create that file inside the Go workspace directory based on the Go environment setup.

> To write a Go code outside the Go workspace directory. Use the Go module instead.

Let's write the first program in the main.go :
```go
package main
import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")
}
```

To execute the program, run this command in your working directory that is used to write that program.

```bash
go run main.go
```

Here it is the output :
```bash
Hello, playground
```

The flow of the program that has already been created is like this:
1. Declare the package main
2. Import the required library (for example the syntax import "fmt" package)
3. declare the main function of the program, the func main() executes all the syntax inside it.
4. From the fmt package, use the function called Println() to write the output to the console
```go
package main //1
import (
	"fmt" //2
)

func main() { //3
	fmt.Println("Hello, playground") //4
}
```

Briefly, The flow of execution uses a top-down approach.

FYI, the Uppercase letter in the method name (Println) means that the function is in a package scope. 

## Write a Go Program with Go Module
The go module can be used to store Go codes and their dependencies (if dependencies are required) outside the Go workspace. This is an example of writing a Go program with a Go Module.

1. Create a new folder with any name, for example, `first-go-program`.

2. Open that directory with a text editor or IDE.

3. Initialize the Go module.

```bash
go mod init first-go-program
```

4. Create a new file called `main.go` then write this code.

```go
package main
import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")
}
```

5. Run the program.

```bash
go run main.go
```

This is the output of the program

```bash
Hello, playground
```

##Variables
The variable is a mechanism to store a value in a certain place. With a variable, we can store any value with a specific type like string, int, or even array. 
Go has many types of value/data that can be stored inside a variable. The list of Go data/value types are :
* Boolean: `bool` (true/false)
* Numeric types : `int`, `float64`, `uint8` ([other numeric types](https://go.dev/ref/spec#Numeric_types))
* String: `string`
* Array:  e.g. `[4]int{1,2,3,4}`
* Slice: e.g. `[]int{1,2,3,4}`

The other types can be checked [here](https://go.dev/ref/spec#Types).

In numeric types, there are two main types: unsigned (uint8, uint 32) and signed (int, float64). The meaning of unsigned is only the positive number that is available in the type. The signed like int and float64 are available for positive and negative numbers.

Here is an example of assigning a value inside a variable in Go.
```go
var name string = "Using var keyword" //1 using var keyword
```
```go
data := 42 //2 using shorthand syntax
```
```go
var (
	a = 1
	b = 2
	c = 3
) //3 using var(..) for declaring multiple variables with ease
```
The complete example of variable declaration can be seen in this code below.

```go
package main

import (
	"fmt"
)

var (
	a = 1
	b = 2
	c = 3
) //declare multiple variables

func main() {
	data := "Box this lap" //declare variable with shorthand syntax
	fmt.Println(a, b, c) //print the value of a,b,c
	fmt.Printf("%T %T %T\n", a, b, c) //print the type of a,b,c variable with %T notation
	fmt.Println(data) //print the value of data
	fmt.Printf("%s %T\n", data, data) //print the value with %s notation and the type with %T notation
}
```
The output of that code :
```bash
1 2 3
int int int
Box this lap
Box this lap string
```

The arithmetic operations are available in numeric types, the operand (the variable involved in the arithmetic operation) must have the same type. For example, the operation between the uint type and with int type cannot be executed because the type is different. 

An example of an arithmetic operation can be seen in this code below
```go
package main

import (
	"fmt"
)

var a int = 10
var b int = 2

func main() {
	d := a + b 
	e := a - b 
	f := a / b 
	g := a * b
	h := a % b //gives the result of remainder between 10 and 2
	
	fmt.Printf("The result of %d + %d equals %d\n",a,b,d)
	fmt.Printf("The result of %d - %d equals %d\n",a,b,e)
	fmt.Printf("The result of %d / %d equals %d\n",a,b,f)
	fmt.Printf("The result of %d * %d equals %d\n",a,b,g)
	fmt.Printf("The result of %d modulo %d equals %d\n",a,b,h)
	
}
```

The output of that code :
```bash
The result of 10 + 2 equals 12
The result of 10 - 2 equals 8
The result of 10 / 2 equals 5
The result of 10 * 2 equals 20
The result of 10 modulo 2 equals 0
```
##Type Conversion
In Go, the value of a certain type can be converted to another type. To convert to the specified type using the destination type followed by "()" (to convert from string to a certain type use strconv.parseType() (example: strconv.parseInt("42", 10, 64)))

To convert int to float64, use the float64 type followed by "()"
```go
a := 42
data := float64(a)
```

To convert string to int, use strconv package with Atoi() function.
```go
a := "43"
data, _ := strconv.Atoi(a)
```
To convert int to string, use strconv package with Itoa() function.
```go
a := 43
data := strconv.Itoa(a)
```
To learn more about strconv package, check this [link](https://pkg.go.dev/strconv)

##Sources
Here are some useful resources to learn more about the Go programming language
* [best practice for writing Go code](https://go.dev/doc/effective_go)
* [Go language specification](https://go.dev/ref/spec)
* [Documentation about Go packages and third-party packages as well](https://pkg.go.dev/)

I hope this article helps to learn the Go programming language. If you have any thoughts or feedback, you can write it in the comment section below.