The first line of code in Go language

 3 minutes to read

The first line of code in Go language

In the previous article, we introduced why we should learn the Go language. In this article, we will write our first line of Go code.

Before writing the code, we need to install the Go language development environment. You can download the installation package from the official website of Go in China, golang.google.cn/dl/. Choose the appropriate package based on your operating system. As mentioned before, Go is a cross-platform language, so there are different installation packages for different platforms. Make sure to select the correct package for your operating system and CPU architecture (386, amd64, arm).

For Windows users, it is recommended to download the MSI installer instead of the zip package, especially for those who are not experienced. The zip package does not require installation but needs manual configuration of environment variables. You can choose the download option according to your preference.

For Mac users, besides downloading the installation package, you can also use the software package manager brew to install Go. It is relatively simple. Just execute the following command:

brew install go

For Linux users, there are various Linux distributions with different package managers. You can install Go using the package manager that comes with your Linux distribution.

Once the Go compiler is installed, open the command line and enter the following command:

go version

If the Go language version is output normally, it would be as shown in the following example on a Mac system

go version go1.21.0 darwin/amd64

Installation successful. Next, let’s start writing our first line of Go code. Create a folder named “hello”, enter the “hello” folder, and execute the following command:

go mod init example/hello

It will create a go.mod file. go.mod is a tool in Golang used to manage dependencies, and we will discuss it in detail later. Create a file named “hello.go” and enter the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Let’s take a look at the meaning of each part:

  • package main: This is a package declaration that tells the compiler that this file belongs to the main package.
  • import “fmt”: This is an import statement that imports the fmt package, which provides functions for formatted input and output.
  • func main(): This is the entry function of the program, and it serves as the starting point of program execution.
  • fmt.Println(“Hello, World!”): This is a function call that invokes the Println function from the fmt package and passes a string parameter “Hello, World!”. The Println function prints the string to the standard output. Therefore, the purpose of this code is to output “Hello, World!” to the console. To run the program, execute the following command.
go run hello.go

the output result can be seen.

Hello, World!

The first program in Go language is completed.

We can directly run the program using the go run command, similar to dynamic languages like Python. Generally, Go programs are compiled before execution. We can compile the program using the go build hello.go command, which will generate an executable program. We can see that an executable program has been generated, with a size of 1.9M on macOS. Compared to a simple “Hello, World!” program in C, the size is indeed larger. By default, the go build command includes debug information and symbol table information. We can add flags to remove these information, using the following command to compile:

go build -ldflags "-w -s" hello.go

After execution, we can see that the size of the generated file becomes 1.3M. Since Go language includes the runtime when generating executable files, the size of the generated executable file is relatively large compared to C programs. The size of a “Hello, World!” program in C is 49k. That’s all for the first line of code in Go language!