Golang from beginner to advanced (Part 1: Hello World and basics)

Tram Ho

1. What is Golang

Go, also known as Golang is an open source programming language (statically type) compiled by Google.

The main purpose of Golang is to support the development of high availability web apps and to make it faster and easier to expand.

Golang can be applied in many fields such as Web Server development, mobile application development or in microservice systems or ERP.

Golang has advantages over other languages ​​such as:

  • Golang is a compiled language, the source code will be compiled into binary code, which is missing in Javascript – NodeJs.
  • With Golang, we will not need to install dependencies from the server because Go has linked all the modules and dependencies into a binary file.
  • Golang uses separate goroutines to save memory and improve application performance.

2. Install

Golang supports all three platforms: Mac, Windows, Linux. You can download the corresponding installer here

Windows

On Windows, after downloading the Go for Windows installation file to your computer, simply double-click the file and proceed to install Go as instructed. By default, the computer will install Go in the c:Go folder, also known as the $GO_ROOT . In addition, the installer will automatically add the c:Gobin to the environment variable on the machine. This directory is also known as the $GO_PATH directory

MacOS

Similar to Windows, you can also install Golang using the installation file with the .pkg extension for computers running Mac OSX. Golang will be installed at /usr/local/go and automatically add the directory /usr/local/go to the PATH environment PATH

Linux

Download the installation file with .tar extension and extract it in /usr/local . Add the /usr/local/go to the PATH environment variable

3. The first Hello World program

After installation is complete, we will embark on code program Hello World “divine”.

To run the program we use the command

If you want to build before running, use the command

4. Variable

Declaring and using variables is extremely important when learning a new language. With Golang, variables can be declared in various ways

Declare a variable

Syntax: var name type

Sometimes for brevity we can use fast declarations

Or how to declare the data type inference itself

Declare multiple variables

We can also declare multiple variables on a single statement.

5. Control command

The syntax of the control command in golang is quite similar to other languages

If …. else

For

or

Also we can use many variables in the for loop

6. Array, Slice

Array

Arrays are collections of elements of the same data type. There are many different ways to declare an array

Array array using range

Note when using arrays
  • Arrays are value types : Arrays in Go are non-referenced value types like other languages. So we can make a copy of the original by assigning a new array with the original one. Any changes to the new array will not affect the original array

  • The size of the array is part of the value type : [3] int and [5] int are different types. Therefore the array cannot be resized.

Slice

As mentioned above, the array has the disadvantage of being unable to resize and transfer values. Slice are born to solve these limitations. Slices are references to existing arrays. Slice is extremely flexible.

First, let’s learn how to create a slice

Modify a slice

Next we need to distinguish the length and capacity of a slice.

  • The length of a slice is the number of elements in the slice. We can call the length of the slice equal: len(slice)
  • A slice’s capacity is the number of basic elements starting from the item the slice was created

Change the length and capacity of the slice

As is known, arrays are limited in length and cannot be increased. So how to add elements to arrays. To solve this problem we use slices and add new elements using the append function.

Refer

https://tour.golang.org/

https://golangbot.com/learn-golang-series/

https://www.tutorialspoint.com/go/index.htm

Share the news now

Source : Viblo