loading...

Golang tutorial - 1 Introduction

nadirbasalamah profile image nadirbasalamah ・4 min read

Golang Tutorial (9 Part Series)

1) Golang tutorial - 1 Introduction 2) Golang tutorial - 2 Constant 3 ... 7 3) Golang tutorial - 3 Control Flow (Selection) 4) Golang tutorial - 4 Control Flow (Iteration) 5) Golang Tutorial - 5 Array, Slice and Map 6) Golang tutorial - 6 Function 7) Golang tutorial - 7 struct and interface 8) Golang tutorial - 8 Error Handling 9) Golang tutorial - 9 Concurrency using goroutine

Introduction

Go Programming language also known as Golang is an open source programming language that 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 avaliable in here : https://golang.org/dl/

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

Golang playground also available for learning the Go programming language : https://play.golang.org/

First Program

To check that Go installed correctly in your local machine, type this command.

go version

To check the Go environment setup, use this command.

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.

Let's write the first program in main.go :

package main
import (
    "fmt"
)

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

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

go run main.go

Here it is the output :

Hello, playground

The flow of the program that already 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 fmt package, use the function called Println() to write the output to the console
package main //1
import (
    "fmt" //2
)

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

Briefly, The flow of execution is using top-down approach.

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

Variables

Variable is a mechanism to store a value into certain place. With variable, we can store any value with specific type like string, int or even array.
Go has many type of value / data that can be stored inside a variable. The list of Go data / value types are :

The other types can be checked in Go language specification : https://golang.org/ref/spec#Types

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

Here it is the example of assigning of a value inside variable in Go.

var name string = "Using var keyword" //1 using var keyword
data := 42 //2 using shorthand syntax
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.

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 :

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 that involved in arithmetic operation) must have a same type. For example the operation between uint type with int type cannot be execute because the type is different.

The example of arithmetic operation can be seen in this code below

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)

}

Output of that code :

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 certain type can be converted to another type. To convert to the specified type use the destination type and followed by "()" (to convert from string to certain type use strconv.parseType() (example : strconv.parseInt("42", 10, 64)))

To convert int to float64, use the float64 type followed by "()"

a := 42
data := float64(a)

To convert string to int, use strconv package with Atoi() function.

a := "43"
data, _ := strconv.Atoi(a)

To convert int to string, use strconv package with Itoa() function.

a := 43
data := strconv.Itoa(a)

To learn more about strconv package, check this link : https://golang.org/pkg/strconv/

Sources

Here are some useful resources to learn more about Go programming language

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

Golang Tutorial (9 Part Series)

1) Golang tutorial - 1 Introduction 2) Golang tutorial - 2 Constant 3 ... 7 3) Golang tutorial - 3 Control Flow (Selection) 4) Golang tutorial - 4 Control Flow (Iteration) 5) Golang Tutorial - 5 Array, Slice and Map 6) Golang tutorial - 6 Function 7) Golang tutorial - 7 struct and interface 8) Golang tutorial - 8 Error Handling 9) Golang tutorial - 9 Concurrency using goroutine

Posted on May 17 by:

nadirbasalamah profile

nadirbasalamah

@nadirbasalamah

A guy who interested in web development and software engineering

Discussion

markdown guide