Go Language Structure
Before we start learning the basic building blocks of the Go programming language, let's first understand the structure of the simplest Go program.
Go Hello World Example
The basic components of a Go language program include the following parts:
Package declaration
Import packages
Functions
Variables
Statements & expressions
Comments
Next, let's look at a simple code that outputs "Hello World!":
Example
package main
import "fmt"
func main() {
/* This is my first simple program */
fmt.Println("Hello, World!")
}
Let's examine each part of the above program:
-
The first line of code package main defines the package name. You must specify on the first non-commented line of the source file which package this file belongs to, such as: package main. Package main indicates an independently executable program, and every Go application contains a package named main.
-
The next line import "fmt" tells the Go compiler that this program needs to use the fmt package (functions, or other elements), which implements formatting I/O (input/output) functions.
-
The next line func main() is the function where the program starts execution. The main function is mandatory in every executable program and is generally the first function executed after startup (if there is an init() function, it will be executed first).
-
The next line /.../ is a comment and will be ignored during program execution. Single-line comments are the most common form of comments, which can be used anywhere with a // prefix. Multi-line comments, also known as block comments, start with /* and end with */ and cannot be nested. They are typically used for package documentation or commenting on blocks of code.
-
The next line fmt.Println(...) can output strings to the console and automatically adds a newline character \n at the end.
-
When an identifier (including constants, variables, types, function names, struct fields, etc.) starts with an uppercase letter, such as: Group1, the object with this identifier can be used by code from external packages (the client program needs to import this package first), which is called export (similar to public in object-oriented languages); if the identifier starts with a lowercase letter, it is not visible outside the package, but it is visible and usable within the entire package (similar to protected in object-oriented languages).
Executing a Go Program
Let's see how to write and execute a Go program. The steps are as follows:
-
Open an editor like Sublime2 and add the above code to the editor.
-
Save the above code as hello.go
-
Open the command line and navigate to the directory where the program file is saved.
-
Enter the command go run hello.go and press Enter to execute the code.
-
If done correctly, you will see the output "Hello World!" on the screen.
$ go run hello.go
Hello, World!
-
We can also use the go build
command to generate a binary file:
$ go build hello.go
$ ls
hello hello.go
$ ./hello
Hello, World!
Note
It's important to note that {
cannot be on a separate line, so the following code will generate an error when run:
Example
package main
import "fmt"
func main()
{ // Error, { cannot be on a separate line
fmt.Println("Hello, World!")
}