Go language structure

Before we start learning the fundamental 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 the Go language include the following parts:

  • Package declaration
  • Import packages
  • Function
  • Variable
  • Statement & Expression
  • Comments

Next, let's look at a simple piece of 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 the various parts of the above program:

  1. First line of codepackage mainDefines the package name. You must specify in the first non-comment line of the source file which package this file belongs to, e.g., package main. package main indicates an independently executable program, and every Go application contains a package named main.

  2. Next lineimport "fmt"Tells the Go compiler that this program needs to use the fmt package (its functions, or other elements); the fmt package implements functions for formatted I/O (input/output).

  3. Next linefunc main()Is the function where the program starts executing. The main function is required in every executable program. Generally speaking, it is the first function executed after startup (if there is an init() function, that function will be executed first).

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

  5. Next linefmt.Println(...)Can output the string to the console and automatically add a newline character \n at the end.
    Using fmt.Print("hello, world\n") produces the same result.
    The two functions Print and Println also support using variables, e.g., fmt.Println(arr). If not specifically specified, they will output the variable arr to the console in the default print format.

  6. When an identifier (including constants, variables, types, function names, struct fields, etc.) begins with an uppercase letter, such as Group1, then objects using this form of identifier can be used by code in external packages (client programs need to import this package first), which is called exporting (like public in object-oriented languages); if the identifier starts with a lowercase letter, it is invisible to packages outside, but they are visible and usable within the entire package (like protected in object-oriented languages).


Execute the Go program

Let's 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 ashello.go

  3. Open the command line and navigate to the directory where the program file is saved.

  4. Enter commandgo run hello.goAnd press Enter to execute the code.

  5. If done correctly, you will see on the screen"Hello World!"The output of the text.

    $ go run hello.go
    Hello, World!
    
  6. We can also usego buildCommand to generate the binary file:

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

Note

It should be noted that{It cannot be placed on a single line, so the following code will produce an error at runtime:

Example

package main

import "fmt"

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