Go: An Introduction

Welcome! Welcome to the course on The Go Programming Language. This course assumes that you already have some programming experience. Without further ado, let's get started.

Please note that we are going to learn Go language in as-needed way. Meaning that we are going to learn the concepts of Go based on requirements and initial understanding. So, it might happen in course that we are going to learn one concept with brief introduction and then again learn that same concept in future with deep dive.

In order to follow this course, you need Go compiler. Go compiler can be easily install from its official website. If you are using Fedora 31, use the following command (because I'm currently using).

$ sudo dnf install golang

This command will install Go compiler on your system with some other tools. You can confirm the installation using,

$ go version
go version go1.13.4 linux/amd64

(Please note, there is no - or -- with command-line arguments). Great! If you get similar output, then you are ready to go further with this course else you need to wait until the Go install on your system.

Now, it is time to write hello, world program in Go.

First Program

Let's touch the file with name hello.go. Yes, Go language has .go extension. Open this file in your favourite text-editor.

Go is compiled language and as usual with any traditional compiled programming language, program must have main() function, an entry point of execution. So, let's add this main() function in newly created hello.go file.

func main() {

}

Go use func keyword for defining function (JavaScript has function, Rust has fn, waiting for fun!). Go compiler executes this main() function. But, this function is empty and we need to print hello, world string on screen. Let's do this using Printf() function.

func main() {
    Printf("hello, world\n")
}

You don't need any explanation for the usage of \n.

But, this Printf() function is not a part of language construct. It is part of fmt package. Actually, package is just a fancy word for standard library. So, we need to import this fmt package first in our program. Importing can be done via import keyword as,

import "fmt"

func main() {
    Printf("hello, world\n")
}

Now, we need to update this Printf() function with fmt.Printf() function as this function is the part of fmt package. So,

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

Finally, add this package main line at the top of this program in the following way.

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

This package main statement automatically make sense for you once you become familiar with Go. For now, just remember that you have to write this line at the top of your program in order to make it more structural and valid.

That's it! This is our hello, world program. It is time to run this program and that can be done using,

$ go run hello.go
hello, world

Here, we are using go compiler and passing the name of the file hello.go with run command. We'll talk more about available commands in future. Before, we go further and learn new concept, let me update the hello.go program little bit and leaving on you to understand what I update.

package main

import "fmt"

func main() {
    fmt.Println("hello, world")
}

Floor is open for questions.

What about semicolons?: Semicolons are optional. If you are writing one-statement-per-line, semicolons are optional. But, if you are writing more than one statement in single line, you have to use semicolon.

Where is compiled executable?: Once you get the output, your executable goes away. If you want the executable, you need to run go build hello.go command. This build command create an executable with same name as program - hello. You can run this program using ./hello command. go run hello.go is two-in-one command.

What is this package main?: I told, you need to wait a little bit to understand this. Once you become familiar little bit with Go, you automatically knows the meaning.

Let's go further with variables.


Soumyajit Pathak picture

Nice and short introduction.

Keep going with the series. Waiting for your piece on stuff like interfaces, goroutines, etc.

KC picture

Thanks. You need to wait for couple of weeks for the mentioned topics! These topics are in queue.