Tech & Engineering
10
min of reading
July 21, 2020

Go language structure

Morvana Bonin
Backend Software Engineer with a strong passion for design patterns, clean code and software architecture.
More about the author

* article elaborated in partnership with Andreia Camila da Silva

In the previous post [But why Go?] we’ve talked about the benefits of the Go language. In this continuation, we’re going to discuss the Go language structure.

GO language structure

To understand the structure of a Go program, we need to analyze its code, so let’s take the hello world from our previous post.

/code

package main

import “fmt”

func main() {

fmt.Printf(“Hello World\n”)

}

/code

Packages

The first line of code is the declaration of a package that is similar to modules and libs in other languages

The packages facilitate the process of division of responsibilities. A package consists of one or more .go files.

Every Go program must belong to a package and that specific program belongs to the “main” package. In this case, the combination of the main package declaration and the main function makes a Go program executable and independent.

Main package and main function

The main package as well as the main function are special. The main package defines an executable program rather than a modular one. In this case, the main package and the main function are the beginning of the program’s execution and are similar to the Java language and the C language itself.

Standard library

The Go standard library has over 100 packages for common input (data input) and output (data output) style tasks, sorting and text manipulation. The fmt package, for example, has both input and output functions, Println being one of the basic output functions.

Compilation and build

As said earlier, Go is a compiled language. This means that it converts a program and all its dependencies into a computer’s machine language.

The run command executes our initial code, as follows:

/code

$ go run hello.go

/code

It should display the following line

/code

$ Hello World

/code

Go handles Unicode natively and this makes it easy for it to process text in any language.

To create a compiled program result, just run go build

/code

$ go build hello.go

/code

This will generate a binary executable file, named hello, which can be run at any time without any additional processes, as follows

/code

$ ./hello

Hello World

/code

References and useful links

https://dzone.com/articles/golang-tutorial-learn-golang-by-examples

https://dzone.com/articles/structure-of-a-go-program

Golang in 20 minutes by Wesley Willians

The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan


Do you want to know more? Talk to one of our specialists, just fill the form below and soon we will get in touch ;)

subscribe to our newsletter with exclusive content.

Click and join the Sensedia News!

Click and join the Sensedia News!

Click and join the Sensedia News!

Thanks for reading!