Go Language Program Structure

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

Go program mainly consists of the following parts:

    • Package Declaration

    • Importing packages

    • Function

    • Variable

    • Statements and expressions

    • Comments

Let's take a look at the simple "Hello World" code that will print the word:

Package Mainimport "FMT" Func Main () {/* This was   my first sample program. */   FMT. Println ("Hello, world!")}

Let's take a look at the various parts of the program above:

    1. The first line of the package body defines the name of the package. This is a must declare for Go program run in what package. The main package is the starting point to run the program. Each package has a path and name associated with it.

    2. The next line of import "FMT" is the preprocessor command that tells the compiler to go to include files in the package fmt.

    3. The next line of Func main () is the start of the program execution.

    4. The next line of/*...*/is ignored by the compiler, and it has been added to the program to add comments. Therefore, such a line is called a comment in the program. Comments are also used//similar to Java or C + + annotations.

    5. The next line of FMT. Println (... ) is to provide another feature that makes the message "Hello, world!" To be displayed on the screen. Here the FMT package has been exported, and the Println method is used to display the message on the screen.

    6. Note the println method. In the Go language, a name if it starts with an uppercase letter for export. Export refers to a function or variable/common access to the corresponding package import.

To execute the GO program:

Let's look at how to save the source code file, and how to compile and run it. Here are the simple steps:

    1. Open a text editor and add the above code

    2. Save File as Hello.go

    3. Open a command prompt and go to the directory to save the file.

    4. Type go run Hello.go and enter to run your code.

    5. If there is no error in your code, you can see the word "Hello world" on the screen.

$ go Run Hello.gohello, world!

Make sure that the go compiler contains the source file Hello.go in the path and directory and runs it.

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.