Golang Introductory Series (ii) a pit to learn the go language needs attention

Source: Internet
Author: User
Tags uppercase letter

In the previous section we have learned about the configuration of the go environment and do not know it, please review the previous article www.cnblogs.com/zhangweizhong/p/9459945.html, this chapter we will learn the basic grammar of the go language need to pay attention to the points.

Go Language Basics Grammar

Go basic Grammar, I do not elaborate here, you can view this article, learn go detailed syntax: http://www.runoob.com/go/go-basic-syntax.html

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

Package main   " FMT "   //import needs to use the package (function, or other element)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!" )}

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 "{" Stand alone, 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!" )}

2. The else in the If...else statement must be on the same line as the If '} ', otherwise the compilation error

int if a < {    FMT. Print ("a<20")} else {    fmt. Print ("a>=20")}

2. Definition of 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

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

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

Package Main

Import "FMT"

Func Main () {   /**/   FMT. Println ("Hello, world!" )}

4. 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 external 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).

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 () {
   .
.
.
}

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

6. Error no new variables on left side of: =, meaning, "there is not a fresh variable on either side!" ”

Func Main () {    int =    b:=    . Fmt. Print (b)}

The solution is: for x,y:= .... In this form, you can simply name one of the variables as a new one.

7. You cannot use the + + self-increment or--decrement operator to initialize variables and assign values to variables

"FMT"func Main () {    int = ten    int = a++    int = c11/>= 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.