Golang Getting started lucky airship Platform Rental series Go language basic grammar and need to pay attention to the pit

Source: Internet
Author: User
Tags uppercase letter

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.

    1. 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!")
}

    1. 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

    1. 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

    1. 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")

    1. 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

    1. 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

    1. 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)

    1. 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.

    1. 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)

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.