Golang getting started
Are you an aspiring software developer in need of guidance? then this material is for you. First you'll learn the basics of programming. The goal is for you to understand some of the concepts used while programming. There are plenty of examples for you to learn from along the way. But let's start of by setting up your computer with the minimum required tools for programming.
Editor, compiler and terminal
EDITOR: The first thing you need is a text editor. There are plenty to choose from. Below are two editors I think are a good starting point. There are lots of others with varying complexity but don't start with those unless you're already familiar with them. The important thing is, it has to be a text editor, Not a word processor like Word.
COMPILER: You'll be using the Go programming language. It and the compiler are available from https://golang.org/dl. Follow the installation instructions carefully and make sure it works.
TERMINAL: Once a program is written you'll want to execute it. Without a graphical user interface you do this in a terminal. The terminal is a small program which enables you to type in commands. When you hit the enter key, the command is executed. The go compiler that you installed earlier is such a command. If you followed the installation instructions thoroughly, you should already have used the terminal so I assume you know where to find it.
Styling conventions
Throughout this material; commands are indicated with a
gray background with a darker left border. Commands start
after the $
character, and other lines are the
output of the command.
$ go version
go version go1.20.3 linux/amd64
Note! when asked to enter a command do not enter the first
$
character, only what is after it. Source code
is presented in two ways; an entire file has a gray background
with a solid border.
package main
func main() {}
whereas partial content is without borders.func main() {}
There is a lot to learn and whenever a section includes
many concepts or longer explanations I'll add a side note with
the Most important thing. Also the material is formated in
such a way that if you choose to print it out there is room
for your own notes on the left hand side.
Ok, you have the
tools up and running and you understand how to read this
material. It's time to learn programming.
Setup working directory
First; use the terminal and create a folder where you will be working.
$ cd $HOME
$ mkdir go-learn
$ cd go-learn
Your first program
package main
func main() {
print("Hello, world!\n")
}
Create a file in your editor and then type in the above
code. Save it as main.go
and run the program in
the terminal as shown below.
$ go run .
Hello, world!
Let's step through each line in the program. The first line tells the compiler that this file is part of a package called main. Go uses packages to group files in larger projects. The main package is special in that it also indicates this should be compiled into a executable command. The keyword func declares a function which we call main. Inside package main, you have to have one function main. The compiler uses it as a starting point when running your command.
In the above example the code is nicely formatted for easy reading but it doesn't have to be. These variations work just fine
package main
func main() { print("Hello, world!\n") }
package main
func main() {
print("Hello, world!\n")
}
You don't want to spend to much time adding spaces and tabs so
your code looks good, let the computer do it for you. For this
you will use the gofmt
command.
$ gofmt -w main.go
Replace main.go
with the name of whatever file you
want to format. Keep your code nicely formattted, it improves
readability for you and others. As you get more experienced
you'll notice that most of your time is spend on reading code
than writing it, so readability is a key point in being
productive. Make it a habit to always keep code perceptible.