Go language structure

Go language structure

Before we start learning basic building blocks GO programming language, let us first understand the structure of the most simple program Go language.


Go Hello World example

Go fundamental component of language has the following components:

  • Package declaration
  • The introduction of package
  • function
  • variable
  • Statement & Expression
  • Note

Let's look at a simple code, the code output "Hello World!":

package main

import "fmt"

func main() {
   /* 这是我的第一个简单的程序 */
   fmt.Println("Hello, World!")
}

Let us look at the various parts of the above program:

  1. The first line defines the package main package name. You must specify the first line of the non-commenting source files belong to which package, such as: package main. independently represents a package main program executed, each application contains Go called main package.

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

  3. The next line func main () is a function of program execution can start. Each function is main executable program must contain, in general are a function of the first execution after the activation (if init () function to execute the function will be).

  4. The next line / ... / is a comment and will be ignored during program execution. Single-line comments are the most common form of comments, you can use single-line comments begin with // anywhere. Note also called multi-line block comments, have to / * begin, and is */ end, not be nested, multi-line comments are typically used to describe a document or comment block snippet into packets.

  5. The next line fmt.Println (...) can be output string to the console, and automatically increase at the end of line character \ n.
    Use fmt.Print ( "hello, world \ n ") can get the same results.
    Print and Println these two functions also supports the use of variables, such as: fmt.Println (arr). Unless otherwise specified, they will be the default print format arr variable output to the console.

  6. When the identifier (including the constant, variable, type, function names, field structure, etc.) to begin with a capital letter, such as: Group1, then the object identifier using this form of the code can be used outside the package (Customer this end needs to be imported program package), which is referred to exporting (public as object-oriented languages); if the identifiers start with a letter, the outer package is not visible, but they are visible throughout the interior of the package and available (such as object-oriented language in private).


Go program execution

Let's look at how to write Go code and execute it. Proceed as follows:

  1. Open editor such Sublime2, added to the above code editor.
  2. Save the above code hello.go
  3. Open a command line and enter the save program files directory.
  4. Enter the command go run hello.go and press Enter to execute code.
  5. If done correctly you will see "Hello World!" Output words on the screen.
$ go run hello.go
Hello, World!

This switched: http://codingdict.com/article/6747

Guess you like

Origin www.cnblogs.com/bczd/p/11974994.html