Once you understand the main reasons to use Go, installed Go on your Windows, Linux or Mac, and built your first program in Go, it's time to dive into the details of the language. On this tutorial we will continue studying our first program in Go to understand some of the important concepts of the language.
If you recall our previous post, in Go, a typical hello world written is as:
import "fmt"
func main(){
fmt.Println("Hello World")
}
Since we already learned how to build and run our first Go program today we'll review each line of our program to understand some of the keywords you'll use while developing in Go.
package main
Any Go program (or project) should one and only have one main function,
but multiple files can be present on the same package, in our case, package main. We'll learn more about packages in the future but to keep it simple, what we
want you to know for now is that, to create your own program in Go, you should
have a function names main() in a packaged named main.
import "fmt"
The next line is import "fmt".
There are two important keywords here:
- import: tells the compiler that we want to import an external dependency into our project
- fmt: the name of our dependency
The import keyword
It's common for our programs to depend on other functionality. In Go, external
functionality is wrapped in packages and the import keyword is
used to do so. In our example, we imported
fmt to have access to the standard
output as we'll see next.
The fmt package
Now stop for a second and think: how did
our first program in Go
printed a line to the console? The answer is that, that
functionality was provided by the Println function of the fmt package
which contains the code necessary to interact with the output of our console.
We'll learn more about it next.
fmt stands for formatting and is the default package for input and output and it's documentation is available here. In our example, it was used to grant access to the Println function required to print contents in our terminal. We'll learn more about it later.
func main() {
Next we enter our main function. In Go, you declare a function as
func <functionName>.
Another important point to note is that, as previously explained, in order to be executed, every Go program has to have one - and only one - main function. It's also common (and recommended but not mandatory) to name the file that contains the main function as main.go.
The { is the operator Go utilizes to create blocks. You'll see it being closed
in the last two lines
fmt.Println("Hello World")
Once our program initializes this is the first thing that gets executed is the line fmt.Println("Hello World"). There are 3 important things in this line:
- fmt.Println: we're calling the function Println from the fmt package
- We're passing the string "Hello World" to it.
Since Go is not an object-oriented language, everything is a function. And accessing the functions of our packages is nothing more than: pkg_name.Function_Name.
If you want to learn more about the package fmt, check this page.
The { and } operators
The { and } operators wrap functions, conditional statements and in our example were used to wrap the main function logic.
Conclusion
On this post we continued our introduction to Go basics and provided a line-by-line explanation of the our first program in Go. But wait! There's a lot more to learn about those keywords that we'll explain in future articles.