This is a creation in Article, where the information may have evolved or changed.
Today began to enter the Golang language learning, Golang language installation and testing is no longer explained here. There is a lot of information on the network to reference.
Learning Golang Language (1): Hello World
Learning Golang Language (2): Variables
Learning Golang Language (3): Type--Boolean and numeric types
Learning Golang Language (4): Type--string
Learning Golang Language (5): Type--array
The first program to learn any programming language is Hello World, and I will not break this tradition. Look at Go's "Hello World".
Package Main
"FMT"//introduction of FMT Library
Func Main () {
Fmt. Println ("Hello world!")
}
Analyze This procedure line by row:
The first line is required. All files written in the Go language start with the package <***>, and the executable file must be the package main for the standalone run;
The second line indicates that the FMT package is added to main. Other packages, which are generally non-main, are called libraries,
The third line is the main function in the program. When the Go program executes, the function that is called first is the main function. This is inherited from C. Here is the function definition of the main function.
Line four calls the FMT package function to print the string to the screen. The string is wrapped by "" and can contain non-ASCII characters.
Note :
A standalone executable Golang program, the package main must appear, immediately following the introduction of the various libraries, and then the various functions, here must have a main function. The main function is the entry for the program.
Compiling and running
Use the command: Go build helloworld.go compile. The HelloWorld executable will be generated in the same directory.
Run:./helloworld
On-screen output: Hello world!
You can also use some parameters at compile time to reduce the size of the compiled file.
go build-ldflags "-s-w" helloworld.go (for reducing the size of the executable program generated by Golang compilation, please reply to 3 view (once slimming for Golang program ) )
The next section will describe the variables, types, and keywords of the Golang language.
--------------------------------------------------------------------------------------------------------------- --
Welcome to follow the code, learn Golang language together.