Introduction to Go - Getting started with Golang
Golang is an open-source programming language developed by Google. It is incredibly fast and easy to learn. This is our first lesson in our introductory tutorial in which we are going to write a few small programs that will help us understand the key concepts of the Go language.
Getting starting with Golang
To be able to follow along, you can either use a playground or install Golang on your computer. The installation package and instructions can be found here.
We are going to start with a simple program that prints "Hello World".
Create a file called hello-world.go
or use the playground link above and add the following lines of code:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
We can run our program from the command line as follows:
$ go run hello-world.go
Hello world
We can also build an executable and run it as follows:
$ go build hello-world.go
$ ./hello-world
Hello world
Now let's try to understand the lines of code we wrote:
- We started by declaring our package:
package main
- Imported a package called "fmt" :
import "fmt"
- Declared the main function:
func main()
- Then in the function block, we used
fmt.Println
to print our "Hello World"
Congratulations! You just wrote your first program in Go.
In our upcoming posts, we will discuss some key basic concepts of Go languages such as variables, loops, conditional statements, functions, structs, arrays, slices, maps, pointers, and concurrency.
We will conclude our tutorial with a CRUD RESTful API. So, stay tuned.
The source codes for the API that we will be building can be found on GitHub page