[Golang] coding and building

2021-09-19 hit count image

Let's see how to write the code and build the code in Golang. And let's see the details about the program.

Outline

In the previous blog post, I will show you how to install Golang and how to write the program that prints Hello, world! by Golang.

In this blog post, I will show you how to write the code that prints Hello, world! again, and the details about the program. You can see the full source code of this post in the following link.

Write program

Let’s create a program that prints Hello, world! by Golang. To develop the program that shows Hello world in the screen, create the main.go file and modify it like the below.

package main

import "fmt"

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

After programming, you can run the program by executing the following command.

go run main.go

And then, you can see the Hello, World! in the screen.

Hello, World!

Details of code

Next, let’s see the details about the prgram that prints Hello, world! created by Golang.

package main

The package is an unit of the code in Golang. When you develop the program with Golang, All programming should be start with package. This package shows the current code belogs to which package.

You can choose any package name, but the package named main is required. package main means there is a starting point, and Golang runs the program from this package.

Golang program consists of a single main pcakge and several other packages.

import "fmt"

This means that the fmt package is loaded. The package is a collection of source code. in other words, the package is a collection of many features. We’ll use the Println function provided by the fmt package to print Hello, World! later. Like this, if you want to use features in the package, you should import the package first.

func main() {
    ...
}

In Golang, when you define a function, you can use the func keyword. You can use any function name, but the function name main is required. The main function is the starting point of the program.

In other words, when you write the program with Golang, you should make the main package, and the main function. If you don’t, Golang can’t find the starting point, so Golang can’t run the program.

fmt.Println("Hello, World!")

It means calling the Println function from the fmt package that we’ve imported to show Hello, World! in the screen.

Build

Now, we can build the programn to make an executable file. To make the executable file, we need to make a Module first. Execute the command below to create Module.

go mod init hello_world

You can use any module name(hello_world), but it should be unique. Normally, the folder name is used.

After creating the module, you can see the go.mod created in the folder.

.
├── go.mod
└── main.go

Next, execute the command below to build the program.

go build

After building the program, you can see the executable file created in the folder like hello_world. (the executable file is dependent on the OS, so if you’re not the macOS user, the file name is different.)

.
├── go.mod
├── hello_world
└── main.go

Execute the command below to run the program that we’ve developed.

./hello_world

You can see the result like the following.

Hello, World!

Completed

Done! we’ve seen the details about the program that prints Hello, world! by Golang. Also, we’ve built the program to make an executable file, and we’ve run the program via the file.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts