Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Structure of Go Language

Structure of Go Language

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


Go Hello World example

The basic components of Go consist of the following parts:

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

  1. The first line of code package main defines the package name. You must indicate which package the file belongs to on the first non-comment line of the source file, e.g., package main. package main represents an independently executable program, and every Go application contains a package named main.

  2. The next line import "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 IO (input/output).

  3. The next line func main() is the function where the program begins execution. The main function must be included in every executable program, and is generally the first function executed after startup (if there is an init() function, it is executed first).

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

  5. The next line fmt.Println(...) can output a string to the console and automatically add a newline character \n at the end.


Using fmt.Print("hello, world\n") gives the same result.
The two functions Print and Println also support the use of variables, e.g., fmt.Println(arr). If not specified otherwise, they 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.) starts with an uppercase letter, such as Group1, then objects using this form of identifier can be used by code in external packages (the client program needs to import the package first); this is called exporting (like public in object-oriented languages); if an identifier starts with a lowercase letter, it is invisible outside the package, but visible and usable throughout the 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 as hello.go

  3. Open a command line and enter the directory where the program file is saved.

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

  5. If the operation is correct, you will see on the screen "Hello World!" the following output.

$ 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!

Note

Note that { cannot be placed on a line by itself, so the following code will produce an error at runtime:

Example

package

main

import

"fmt"

func

main

()

 

{

 

// error, { cannot be on a line by itself

    fmt

.

Println

(

"Hello, World!"

)

}