Basic stuff most beginners miss- Learn how to use Golang with the hello world code

Image for post
Image for post

In this tutorial I will be talking about how go works, by looking into the 6 lines of the hello world code. Some tutorials might not explain in detail what each line means, and jump right into coding. But here, I’m going to make you understand what’s going on behind the scenes, when you run this simple code, by explaining some very basic programming concepts.

  • Compiling
  • Packages
  • The Hello World Code
  • The Go Workspace
  • Running your Code

Compiling

To understand how go works, we first need to understand what it means to “compile” code. Programming languages that we commonly use, like Golang, C, C++, JAVA, python… these are “high-level languages”, meaning that they are human readable.

High-level languages
High-level languages
Examples of high-level languages

But when a machine is trying to execute a program, it needs to be in a form that a machine can read which would look like a bunch of 0 and 1s. So, we would have to convert human readable code into machine language, and this step is called compiling.

A diagram of compiling
A diagram of compiling

Let’s say you are making a calculator program. You’ve written some code into several files that are nicely organised in a directory. Then when you’re finished, you compile it, and then the compiler is going to change your human-readable code into machine readable code and spit out an executable file. You can run the calculator program by running this executable file.

compiler
compiler

Packages

In golang.org, there’s a page called “How to write Go code”. It says there, “A package is a collection of source files in the same directory that are compiled together”. Nothing difficult here. Source file just means the code that you wrote and saved in a file, and we already know what compiling is, so we can understand this definition, which is basically just saying that a package is a bunch of .go files in a directory, that will be compiled together.

Image for post
Image for post
https://golang.org/doc/code.html

In Go, every file should be included in a package. So even if you are writing a very simple piece of code, if you want to do it the standard way, you would put the file inside a directory which would be placed under the src directory of your GOPATH. The name of the package will be the name of the directory.

packages in golang
packages in golang

The reason why we use packages is to reuse other people’s code so that we don’t have to write everything from scratch every time we code something. If we organise our code into packages, it is easier to reuse them in other packages and also help us maintain source code.

Image for post
Image for post

For example, If we want to build a calculator app, we don’t have to write out all the mathematical functions, we just need to use a package that has those mathematical functions and import it in our code.

Image for post
Image for post

Hello World!

Now that we have some basic understanding of what compiling is, and what packages are, let’s start looking into the hello world code.

Golang hello world — package main
Golang hello world — package main

Now, the first line, “package main” means that this .go file belongs to a package called main. Remember how I said all .go files need to belong to a package? All files that have .go as an extension should have “package something” in the first line. We need this package to be an executable program so we have to name this package “main” which means there is going to be an entry point of execution in this package, and this is a rule. If you want your code to run as a stand alone program it needs to include a package called main.

Image for post
Image for post

Now let’s go to line 5. Whenever we want to write code and run it, we have to tell the machine where the execution should start. We do that by writing a function called main, like in line 5. The machine will look for a function called main to find the entry point of the program, which will be the code inside the curly brackets. A function is a block of code that can do a specific task and can be reused here and there. You can declare a function with any name by using func. But in this case you need to name it main, because you’re telling the machine that this is where your code starts.

Image for post
Image for post

Now let’s look at line 3. import fmt means, bring in a package called fmt into this package which is the main package. So basically here, with line 1 and line 3, we are saying “Hey, this file is in the main package, and I also want to include another package called fmt into this main package. fmt is a standard package provided by go, and inside this package, there is a function called Println, and because we imported fmt, we are able to use the function Println, by using fmt.Println in line 6. Println is a function that takes in whatever’s in the brackets, and shows it to us on the terminal window.

To sum up, line 1 is saying this file will be in a package called main, and it needs to be called main because it includes the entry point of the program. Line 5 is the actual starting point, and we know that because it’s the main function. Line 3 is there because we need to import the fmt package to use the Println function on line 6 which is going to let us print hello world on the terminal.

Go Workspace

Image for post
Image for post

Inside the GOPATH, you should make three directories: src, bin and pkg. After you first install go, type go env into the command prompt to check your GOPATH. Normally it should be (your home)/go. If you don’t have the go directory like in your GOPATH, you will have to create it, and inside to go directory, create src, bin and pkg.

Image for post
Image for post

We will store the code that we write in src. Normally right under src, there will be repositories, which is like a version controlled workspace, and under that will be subdirectories, which will be packages. Mostly you will generally create a new subdirectory in your repository to write code for each separate Go package.

Image for post
Image for post

The bin directory will hold executable files which are called binary files. After we compile a package, the compiler will output an executable file which will be found in the bin directory.

Image for post
Image for post

The pkg directory will store non-executable packages. These packages don’t have a main so you cannot turn them into an executable and run them. Instead you can import them into other executable packages and use them.

So that’s a brief explanation of the go workspace, Now let’s talk about compiling and running source code.

Running your code

To run your code, first you need to compile it, and there are three ways to do this: go run, go build and go install.

Let’s see what they are.

Image for post
Image for post

If you install Go, you can use several programs that can help you build or test your source code. “go” is one of the programs you can use. To use the go program, simply type in go in the command prompt, and after that, put a space, and then write the command you need to use.

Image for post
Image for post

Let’s see what kind of commands you can use. You can see a list of commands by typing in “go help”. Or you can also just check out the go tools reference. Among the commands in that list, run, build and install are the ones that can compile your code. In the last video, we already tried out some commands here like go env, and go version, which you can also see in this list.

Image for post
Image for post

The most simple way to compile and run your source code is to simply use “go run”. Let’s say we saved the hello world file as main.go inside a directory first_tutorial inside the src directory.

Image for post
Image for post
Image for post
Image for post

Go to the first_tutorial package directory and simply say “go run main.go”. It will run the code and show “Hello World” in the terminal. But remember how I said that if you compile a program, it’s going to output an executable file? if you use the go run command, it’s not going to show the executable. The run command will output the executable file as a temporary file and delete it after executing.

If you actually want to output an executable file, which would be an exe file in windows and a binary file in mac, you would have to use the go build command.

Image for post
Image for post

In that case, after you build your code, you will see the executable in the current directory. If you want to run that executable, just type the name of the file in the command prompt.

Image for post
Image for post
Image for post
Image for post

The third one is go install. This will also compile your code but instead of leaving the executable file in the current directory, it will put the file in the bin directory. If you install your package, you can run that executable file in the bin directory from anywhere, if you have the bin path added in your environment variable.

Wrapping up

So far we looked into compiling, packages, the hello world code, the go workspace and how to run your source code. I guess you have a better understanding about how go works now. Knowing how all this works is going to help you build a strong foundation. I’ve even saw some tutorials that teach in the go playground. it’s easy because you don’t need to set up anything. This will let you start coding right away. But there’s a downside, if you don’t have that initial struggle setting things up, using the command prompt and using go tools, you miss the opportunity to get a deeper understanding of basic programming concepts like packages and compiling.

I am a tech enthusiast, workaholic, grad student, academic tutor, cat & dog lover, constant learner, coffee addict and an idealist. Yes, both cats && dogs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store