Go language lucky airship platform rental QQ2952777280 "Word Fairy Source Forum" hxforum.com "Papaya Source Forum" papayabbs.com Basic grammar
It is better to knock out one by one against the above example, so that it works best.
Here is the basic structure of a Go program, including (package declaration, Introduction package, function, etc.)
Copy Code
Package Main//defines the packages name, which represents a program that can be executed independently, and each Go application contains a package called Main.
Import "FMT"//imports the packages (functions, or other elements) that need to be used
Func Main () {//The entry function of the program. The main function is what each executable program must contain, typically the first function executed after startup.
Fmt. Println ("Hello, world!")
}
Copy Code
Go language of the pit of attention
No matter what you learn, you will encounter all kinds of pits at first. Here's a summary of the various pits encountered in the process of learning the go language.
- People who write C # will have "{" A separate line, but this is wrong in go "{" must be more method body on the same line. The first time I wrote go, I made this mistake, and I didn't know where the mistake was.
Func Main () {
Fmt. Println ("Hello, world!")
}
- The else in the If...else statement must be on the same line as the If '} ', otherwise the compilation error
Copy Code
var a int = 30
If a < 20 {
Fmt. Print ("a<20")
} else {
Fmt. Print ("a>=20")
}
Copy Code
- The definition of the package name. You must declare the package name in the first line of the non-comment in the source file, for example: packages Main. The package main represents a program that can be executed independently, with each Go application containing a bundle called Main.
Package Main
- In the Go program, a line represents the end of a statement. Each statement does not need to be semicolon-like other languages in the C family; End, because all of this work will be done automatically by the Go compiler.
If you intend to write multiple statements on the same line, you must use; , but we do not encourage this practice in practical development.
Fmt. Println ("Hello, world!")
Fmt. Println ("www.fpeach.com")
- The main () function is the one that each executable program must contain, typically the first one executed after startup. However, there can be only one main () function in each package, otherwise it will report the main redeclared in this block previous declaration at. The error.
Copy Code
Package Main
Import "FMT"
Func Main () {
/ This is my first simple program /
Fmt. Println ("Hello, world!")
}
Copy Code
- When identifiers such as functions and structures start with an uppercase letter, such as: GetInfo, an object using this form of identifier can be used by the Code of the outer package, which is referred to as an export (like public in an object-oriented language); If the identifier starts with a lowercase letter, it is not visible outside the package. But they are visible and usable within the entire package (like protected in object-oriented languages).
Copy Code
Public functions, which can be used by the code of the external package
Func Test () {
.
.
.
}
Private function, inside of the package is visible,
Func test2 () {
.
.
.
}
Copy Code
- Identifiers are used to name program entities such as variables, types, and so on. An identifier is actually a sequence of one or more letters (A~Z and A~z) numbers (0~9) and underscores _, but the first character must be a letter or an underscore and not a number.
The following are invalid identifiers:
1AB (starting with a number)
Case (keywords for Go language)
A+b (operator is not allowed)
- Error no new variables on left side of: =, meaning, "there is not a fresh variable on either side!" ”
Func Main () {
var b int = 20
B: = 30
Fmt. Print (b)
}
The solution is: for x,y:= .... In this form, you can simply name one of the variables as a new one.
- You cannot use the + + auto-increment or-decrement operator to initialize variables and assign values to variables
Copy Code
Package Main
Import "FMT"
Func Main () {
var a int = 10
var b int = a++
var c int = 20c = a++fmt.Print(a, b, c)
}