Although currently rated in 12th position in TIOBE Programming Community Index, Go has had its ebbs and flows throughout the years, and is still gaining popularity.
Once widely adopted at Google where it was first developed, it’s making rounds in the developer community as a must-learn for anyone who wants to work at Google or dive deeper into Google tools. One thing is indisputable: Go has a lot to offer thanks to its clear syntax and robust standard library.
In this part of our Go blog series, I’m set to guide you through your first steps in Golang and the basics of this programming language.
For more information on how and why Go was created and how it compares to its long-time rival, React, visit previous parts of the series:
Golang 101: All the Basics You Need to Know
Go vs. Python: Key Differences Explained
Here’s a list of topics that I’ll discuss in the article below:
1. What Do You Need to Start Using Go?
3. Why Is It Worth Learning Go?
4. How to Start Programming in Go?
For all its intricacy, Go is relatively easy to learn thanks to one feature: a minimalistic syntax. It’s limited to a few crucial statements that you can readily memorize. This also means that the syntax is clean and you can follow it without any issues and not spend too much time looking things up as you learn.
Similarly to C and C++, Go is also a compiled language which means that you need to run source code through a compiler - this feature makes Golang much faster than Javascript.
As you make your first steps in Golang, you have two options on how to develop your skills:
Go is a statically typed programming language that helps reduce the number of bugs and reduces the time spent on debugging. Additionally, type declarations serve as automatically-checked documentation. Here are a few more features of Golang:
You may wonder, why should you learn Go at all.
That’s a very good question. After all, there are hundreds of computer programming languages, old and still in use such as Cobol or C, and new ones like Reason. Golang is a relatively new language (read about its origin story in our Golang - All the Basics that You Need to Know article), but it’s already mature and widely used by large companies - of course, the largest being Google, the creators of this language.
You can also check a case study indicating usage of this language by Google:
New Case Studies About Google’s Use of Go
Go has turned out to have a much broader reach than we had ever expected. Its growth in the industry has been phenomenal, and it has powered many projects at Google.
Okay, but why should you learn Go, to be specific? Here are some of the reasons:
It’s fast: Check benchmarks for the most popular server-side languages such as Node.JS, Python, Java, and PHP.
It’s simple: in order to write a concurrent function you just need to add the `go` keyword before the function.
It’s lightweight: goroutines (threads) take only 8 kilobytes and you can have thousands of them.
Zero dependency to write web server - you will need just the standard library `http` and a few lines of code (extra tip: try this in Node.JS).
Now you know why you should learn and use Go, let’s get coding. Our first step will be installing Go - here’s how to do it on different operating systems.
How to install Go on Windows
Here's detailed documentation: https://go.dev/doc/install
$ go version<
How to install Go on OSX
/usr/local/go
. The package should put the /usr/local/go/bin
directory in your PATH
environment variable. You may need to restart any open Terminal sessions for the change to take effect. $ go version
How to install Go on Linux
/usr/local/go
folder (if it exists), then extract the archive you just downloaded into /usr/local,
creating a fresh Go tree in /usr/local/go
:
$ rm -rf /usr/local/go
tar -C /usr/local -xzf go1.19.linux-amd64.tar.gz
sudo
)./usr/local/go
tree. This is known to produce broken Go installations.PATH
environment variable.$HOME/.profile
or /etc/profile
(for a system-wide installation):
export PATH=$PATH:/usr/local/go/bin
Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as a source $HOME/.profile
. $ go version
Alright, you are good to go and ready to write your first application.
If you are using Go on your local machine, create a file with `go` extension (example.go
) and open it via VS Code. If you are using go playground erase all default code from there.
Our app will look like below:
```
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
```
But let's see in more detail what is going on there!
First, we need to add the package name, because every Go app consists of packages. The entry and the most important package indicate from where Go should start an app, executing code, so write on the top of file: `package main
`
Then we need to import the standard library build into Go itself and it’s called `fmt` - this package simply allows you to print whatever you would like to the variable, output (terminal, file, etc.), so type: `import “fmt”
`.
Now we need our main function that will execute code that we will write, and again the function needs to be called `main`. This is how Go will know that it should start code execution from here, so type: `func main() {}
`
In order to print the words” `Hello World` we need to use our imported library and method called: `Println
` this will just simply print line to our default output which is terminal, so inside the main function type:
```
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
```
Yes, you’ve just created your first app! In order to see what will produce and if it is working - also if you are using Go playground - click the `format` button. It will automatically check if formation is adequate and if there aren’t any errors in a code. Once the code is checked and you don’t need to fix any issues, then simply click the `run` button. You will see the output on the terminal below.
If you are using a local environment in a VS Code in the built-in-terminal type: `go run main.go`. The program will then execute and you should have the same result in your local terminal.
Congrats, you’ve successfully built your very first Go program!
The next step you should take is to do some more tutorials (text or video) there are many of them, including the free ones on the official go website or the unofficial `Free Code Camp` Youtube channel or paid tutorials on Udemy.com. Below you can find a couple of useful links:
Learn Go Programming - Golang Tutorial for Beginners
Learn Go Programming by Building 11 Projects – Full Course
If you have any problems or questions, you can also join the wonderful Golang community on Slack: Gophers.