GO language Structure

Source: Internet
Author: User

Before we start learning the basic building blocks of the Go programming language, let's first understand the structure of the simplest program of the go language.

Go Hello World Instance

The basic components of the Go language are the following sections:

  • Package Declaration
  • Introduction Package
  • Function
  • Variable
  • Statements & Expressions
  • Comments

Now let's take a look at the simple code, which outputs "Hello world!":

 PackageMainImport "FMT"Func main() {   / * This is my first simple program * /FMT.Println("Hello, world!.")}

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

  1. The first line of code packages main defines the package name. You must indicate which package the file belongs to 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.

  2. The next line of import "FMT" tells the Go compiler that this program needs to use the FMT package (the function, or other elements), the FMT package implements the format IO (input/output) function.

  3. The next line, func main () , is the function that the program starts executing. The main function is what each executable program must contain, typically the first function executed after startup (if there is an init () function, the function is executed first).

  4. The next line of/*...*/is a comment, which is ignored when the program executes. Single-line comments are the most common form of annotation, and you can use a single-line comment at any point, starting with//. Multiline comments are also called block annotations, which begin with/* and end with a/* and cannot be nested, and multiline annotations are typically used for package document descriptions or comment blocks of code snippets.

  5. FMT. Println (...)   can output a string to the console and automatically add a newline character \ n at the end.  
    Use FMT. Print ("Hello, world\n") can get the same result.   the
    Print and PRINTLN functions also support the use of variables, such as FMT. Println (arr). If not specifically specified, they will output the variable arr to the console in the default print format.

  6. When identifiers (including constants, variables, types, function names, structure fields, and so on) start with an uppercase letter, such as: Group1, an object using this form of identifier can be used by the code of the external package (the client program needs to import the package first), which is known as an export (as in object-oriented languages). public), identifiers are invisible to the package if they start with lowercase letters, but they are visible and available within the entire package (like private in an object-oriented language).

Execute Go Program

Let's look at how to write the Go code and execute it. The steps are as follows:

  1. Open the editor, such as Sublime2, to add the above code to the editor.

  2. Save the above code as hello.go

  3. Open the command line and go to the directory where the program files are saved.

  4. Enter the command go run hello.go and press ENTER to execute the code.

  5. If the operation is correct, you will see the output of the word "Hello world!" on the screen.

$ go Run Hello.GoHello,  World!

GO language Structure

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.