Let's Go Together (iii)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the previous few, we have set up the go language development environment, from today on formally into the Go Language programming learning.

First, go Language program basic structure and external package import

First look at the basic structure of the Go Language program:

The above is the most basic structure, the main function in the import of the FMT package, call the package's printf function, print the string to the console.

Where import "FMT" can also specify an alias for the imported package, as follows

Import format "FMT"

The FMT package is given the alias format so that it is changed to format when it is called. Printf ("Hello world!")

So when do I need to use aliases? For example, you just use the FMT variable in the program, then it conflicts with the FMT package name, you can use aliases in this case, or the two packages you want to import are exactly the same name, and you can alias one of them.

Of course, it's best not to alias the package, making it easier for others to read your code.

In the alias, one of the more special is "." To import the package into the global package, for example, to change the

Import. "FMT"

Then you call the printf () function directly, and you don't need to specify the package name.

Second, the entrance function

As an example of the short program above, in the go language, the definition function uses the keyword: func

For example, the main function defined above, which is the default entry function of the Go Language program: Func Main ()

The function body, like most languages, is wrapped with a {} pair of parentheses.

Third, the disappearance of the ";" Number

If you are careful and you have used languages such as c,java,c well, you will find that in the go language, the statement end is not written ";". Yes, not forget to write, in the go language does not have to write. Unless you use the FOR Loop statement, you need to use it in the loop condition as follows:

For i = 0;i < 100;i++{

Fmt. Printf ("%d", i)

}

Iv. disappeared "(", ")" No.

Observing the program above, you will be surprised to find that the for statement is not a little bit what? Indeed, there is less "(" and ")", but you are now using go instead of C or Java. So be assured that you can do so boldly later, and you have to do this, because if you add "(" and ")", it will cause syntax errors in the go language, causing the compilation to fail. In addition to the For statement, you do not need to add a "(", ")" number in other statements such as the IF statement. If you're not used to it now, it's okay, because you'll have to learn these statements later.

Five, not related to the study of this article, purely occupies a position

Originally also want to write the declaration of variables, but, and so afraid to write the article too long, for me so impatient people, see a very long article, probably is all at once, and then because an article did not read very uncomfortable. Therefore, continue to maintain the short principle of this series, do not continue to write, variable declaration left to the next article.

author: Sirk   
source: http:// Www.cnblogs.com/vimsk
this edition The right to the author and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

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.