01-Getting Started
Main topics:
- Go installation (Windows, Linux, Raspberry Pi)
- Hello world program
- How to compile and execute a go program
- .exe vs ELF file formats
1. Go Installation
- To install: type following in the terminal
-
To check installation:
type
go version
Expected output:
The default command sudo apt install golang-go
may not install the version that you are looking for.
For example, I got golang 1.19 version installed with the above command.
However, I needed version 1.22 or higher. Following is the procedure.
-
Remove any existing installation:
-
Download Go 1.22 (ARM64 build)
-
Extract to
/usr/local
-
Add go to path
Append following line to the end of ~/.bashrc
(for bash shell only)
source ~/.bashrc
- Verify installation:
We should see
go version go1.22.2 linux/arm64
- Download MSI installer from Go website
- Open the MSI file you downloaded and follow the prompts to install Go. By default, the installer will install Go to Program Files or Program Files (x86). You can change the location as needed.
-
Open the command prompt (or PowerShell)
type
go version
Expected output:
2. Hello World
This chapter describes how to write your first program in Go language.
Following is a standalone program in Go
In this exmple, we are printing some statements. We use Println and Print
- fmt.Print() prints output without a newline at the end.
- fmt.Println() prints output with a newline.
Create a file with any name of your choice, e.g. helloGo.go and copy following contents in it.
A note on comments
Comments in Go can be single-line or multi-line, similar to how they are used in C++ or Java.
3. How to build/run (two methods)
Method 1: Using go run <filename>.go
This method is useful when, we are developing or testing code quickly (useful during development stage only).
- Open the terminal.
- Run the program by typing
go run helloGo.go
The above command:
- Compiles and immediately runs the program.
- Does not keep the binary after execution (it's stored in a temp directory and deleted automatically).
Method 2: Generating a binary first
This method is (more appropriate for for deployment); It does following:
- compiles the helloGo.go file.
- produces a binary executable in the current directory (e.g., helloGo or helloGo.exe on Windows).
- does not run the program automatically.
- Open the terminal.
-
Compile the program by typing go build helloGo.go
An executable file is generated with name helloGo
-
Run the program by typing its name ./helloGo
Executable formats (exe and ELF)
-
In Windows, the generated binary is in .exe format, which is the standard executable format for Windows. Internally, this format is based on the PE (Portable Executable) structure.
-
In Linux, a different executable format is used, called ELF (Executable and Linkable Format).
-
ELF is a standard file format for executables, object code, shared libraries, and core dumps in Unix-like systems (such as Linux and BSD).
-
It is used by the Linux kernel and dynamic loader to understand how to load and run programs.
-
An ELF file is a compiled binary that contains machine code and metadata. It can be directly executed by the CPU, with the help of the kernel and system loader.
To check file format on linux:
- type
file server
(server is the filename of the binary generated by go) - Something like following is displayed:
server: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b6cefb13e943ed5bf39cfb7e48978117d5b04f8c, with debug_info, not stripped
4. Semicolons(;) in Go
- Semicolons are required by the Go language (internally). But you usually don’t write it yourself — the Go compiler inserts it for you automatically during parsing.
When you MUST write semicolons
- If you try to write multiple statements on one line without ;, it will cause a compile error:
So, in practice:
- Semicolons are required by the spec, but Go handles them for you automatically
- You should avoid writing them manually, unless you're writing multiple short statements on the same line (which is not recommended)
5. Points to remember
- Go comment style is same as C/C++.
- A standalone application must inclue **pacakge main" statement on top
- fmt.Println("Hello World") is equivalent of printf statement in C/C++
- Code indentation is not required, but highly recommended for readability. We can use tabs or spaces (same as in C/C++ or Java)
- for prints and some other input/output statements, golang package is fmt which must be included
- starting curly bracket { MUST be one same line as the function name. Unlike C/C++ or Java, we cannot write it to the next line.
- semicolons are optional in Go, unless you are writing multiple statements on a single line (which is discouraged)
6. Check your understanding
What package should your main executable Go program be in?
Ans: package main
What is the significance of the main function in a Go program?
Ans: It’s the entry point where program execution starts.
How do you import other packages into your Go file?
Ans: Using the import keyword followed by the package name(s).
What does fmt.Println do?
Ans: Prints text with a newline to the console.
What package is needed to use Println?
Ans: fmt
What is the difference between Print and Println from the fmt package?
Ans: Print doesn’t add a newline; Println adds a newline at the end.
Is the main function mandatory for all Go programs?
Yes, for executable programs (i.e main package) No, for other packages
How do you write a comment in Go?
Ans: Use //
for single-line comments, and /* */
for multi-line comments
Can the main function take parameters or return values?
Ans: No, main must have no parameters or return values.
What are alternate ways to pass command line arguments and return code?
Ans: If you want to handle command-line arguments,
- use the os.Args slice instead.
- To signal an exit status, use os.Exit(code).
(Note: They are not covered in this lesson)
What happens if you try to declare main with a capital letter like Main?
Ans: It won’t be recognized as the entry point; program won’t run as expected.
How do you run a Go program in console?
Ans: Use go run filename.go
or build with go build <inputfile.go>`<outfile>
and run the generated executable by typing its name in the console
What is wrong (if any) with the following code?
Ans: Go places the opening { on the same line as the function declaration; the correct code is following.