Go - Program Structure

Before we start learning the basic building blocks of the Go programming language, let's first understand the structure of the simplest program in the Go language.

Go Hello World example

The basics of the Go language have the following components:

  • Package statement
  • Introduction package
  • function
  • variables
  • statement & Expression
  • Comment

Let's take a look at the simple code that is output."Hello World!":


package main

import "fmt"

func main() {
   /*This is my first simple program*/
   fmt.Println("Hello, World!")

Let's take a look at the various parts of the above program:

  1. The first line of code package main defines the package name. You must indicate in the first line of the source file that the file belongs to the first line, such as: package main. Package main represents a program that can be executed independently, and each Go application contains a package called main.

  2. The next line import "fmt" tells the Go compiler that this program needs to use the fmt package (function, or other elements), and the fmt package implements formatted IO (input/output) The function.

  3. Next line func main() is the function that the program starts executing. The main function is required for every executable. It is generally the first function to be executed after startup (if there is an init() function, it will be executed first).

  4. The next line /*...*/ is a comment and will be ignored during program execution. Single-line comments are the most common form of comments, and you can use single-line comments starting with // anywhere. Multi-line comments, also called block comments, have started with /* and end with */ and cannot be nested. Multi-line comments are generally used for package descriptions or coded blocks of code.

  5. Next line fmt.Println(...) can output a string to the console and automatically add a newline character \n at the end.
    Use fmt.Print("hello, world\n") to get the same result.
    The Print and Println functions also support the use of variables such as: fmt.Println(arr). If not specified, they will output the variable arr to the console in the default print format.

  6. When identifiers (including constants, variables, types, function names, structure fields, etc.) start with an uppercase letter, such as: Group1, objects that use this form of identifier can be wrapped externally The code used (the client program needs to import this package first), this is called export (like public in the object-oriented language); if the identifier starts with a lowercase letter, it is invisible to the outside of the package, but they are The interior of the entire package is visible and usable (like protected in an object-oriented language).

Execute the Go program

Let's take a look at how to write Go code and execute it. The steps are as follows:

  1. Open an editor such as Sublime2 and add the above code to the editor.

  2. Save the above code as hello.go

  3. Open the command line and go to the directory where the program files are saved.

  4. Enter the command go run hello.go and press Enter to execute the code.

  5. If you do this correctly, you will see the output of "Hello World!" on the screen.

    $ go run hello.go
    Hello, World!
  6. We can also use go build Command to generate a binary file:

    $ go build hello.go
    $ ls
    hello    hello.go
    $ ./hello 
    Hello, World!


It’s important to note that { can't be placed on a single line, so the following code will generate an error at runtime:


package main

import "fmt"

func main()  
{  // Error, {cannot be on a separate line
    fmt.Println("Hello, World!")