Mahdi.Kh
September 21, 2026
Go programs share a similar structure. Here, we get familiar with the overall structure of a Go program and learn some basic concepts.
Go programs have a simple, understandable structure. This structure includes a few basic parts, which we'll go over below:
In Go, we use directories (folders) and files to organize code. Each directory (folder) can have one or more files.
Each directory corresponds to one package, and the content of each file starts with a package statement. The package statement shows which package the file belongs to, so the name we specify with the package statement should preferably match the name of the directory the file is in.
As in the image above, suppose we have two directories (folders) named foo and bar. Each of these directories contains a number of Go files. The first line of each file specifies the name of the package that file belongs to. In most cases, the package's name should match the name of the directory the source code file is in.
If we're in package foo and want to use things we defined in package bar, we first need to import package bar. We can do the importing either individually or as a group.
Every Go program has a function named main, which sits inside a package named main. Go programs start running from this main function.
Comments are textual notes placed in a program's code. Comments are used to document code, explain how it works, or note down important reminders. In Go, comments come in two forms: single-line and multi-line.
Single-line comments start with two slashes (//). On any line of code, everything after these marks, up to the end of that line, is treated as a comment.
Multi-line comments start with /* and end with */. Anything placed between these two marks is treated as a comment.
In the Go programming language, letter case matters, because Go treats uppercase and lowercase letters as different — in other words, it's case sensitive.
To run the program, we can use go run. We need to run this command in the directory (folder) that our source code is in.
Compiling is the process by which a program's code is turned into machine language so that it's understandable and runnable by the computer. This process is carried out by a program called a compiler.
To compile Go programs, we can use the go build command.
Running the command above compiles the program, and the compiled file, named app, is placed in the current directory (folder). You can use any name you like instead of app. The -o flag helps you specify the name of the output file.
You can also run the build command as follows:
Running the command above compiles the program and creates the final file, named main. This file can be run independently, without needing the program's source code. To run it, we do the following:
In this part, you got familiar with the overall structure of a Go program, and you also learned how to run and, finally, compile your Go programs.
Previous part: Setting Up the Development Environment | Golang basics
Next part: Variables | Golang basics