Go language structure
Before we start learning the basic building blocks of the GO programming language, let us first understand the structure of the simplest program in the Go language.
Go Hello World Example
The basic components of the Go language have the following parts:
Package declaration
Introduction package
Function
Variable
Statement & expression
Comments
Next let’s look at a simple code that outputs "Hello World!":
package main import "fmt" func main() { /* 这是我的第一个简单的程序 */ fmt.Println("Hello, World!") }
Let's take a look at each part of the above program:
The first line of code package main defines the package name. You must indicate which package this file belongs to in the first non-commented line of the source file, such as: package main. package main represents an independently executable program, and every Go application contains a package named main.
Next lineimport "fmt" tells the Go compiler that this program needs to use the fmt package (functions, or other elements), which implements formatted IO (input/output) function.
The next line func main() is the function where the program starts executing. The main function must be included in every executable program. Generally speaking, it is the first function to be executed after startup (if there is an init() function, this function will be executed first).
The next line /*...*/ is a comment and will be ignored when the program is executed. 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, start with /* and end with */, and cannot be nested. Multi-line comments are generally used for package documentation descriptions or code snippets commented into blocks.
Next line fmt.Println(...) You can output the string to the console and automatically add the newline character \n at the end.
Use fmt.Print("hello, world\n") to get the same result.
The two functions Print and Println also support the use of variables, such as: fmt.Println(arr). If not specified otherwise, they print the variable arr to the console in the default printing format.When the identifier (including constants, variables, types, function names, structure fields, etc.) starts with an uppercase letter, such as: Group1, then use this form of identifier The object can be used by the code of the external package (the client program needs to import this package first), which is called export (like public in object-oriented languages); if the identifier starts with a lowercase letter, it is not available outside the package. Visible, but they are visible and available within the entire package (like private in object-oriented languages).
Execute Go program
Let’s take a look at how to write Go code and execute it. The steps are as follows:
Open an editor such as Sublime2 and add the above code to the editor.
Save the above code as hello.go
Open the command line and enter 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 "Hello World!" output on the screen.
$ go run hello.go Hello, World!