Latest web development tutorials

Go Language Structure

Before we begin to learn a programming language GO fundamental building blocks, let us first understand the structure of language Go easiest program.


Go Hello World Examples

Go composition based language has the following components:

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

Let us look at simple code, the code is output "Hello World!":

package main

import "fmt"

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

Let's look at each part of the above program:

  1. The first line defines thepackage mainpackage name. You must specify the source file in the first line of the Central African annotated files belong to which package, such as: package main. package main represents an independent program execution, each Go application contains a package named main.

  2. The next lineimport "fmt"Go tell the compiler that this procedure requires the use fmt package (function, or other elements), package fmt implements formatted IO (input / output) functions.

  3. The next linefunc main ()is a function of the program begins execution. Each main function is an executable program must contain a function in general are performed after the first start (if init () function will perform this function).

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

  5. The next linefmt.Println (...)can string output to the console, and automatically increase the last newline 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). If not specified, they will default print format variable arr output to the console.

  6. When identifiers (including constants, variables, types, function names, structure fields, etc.) begin with a capital letter, as in: Group1, then the use of this form of object identifier can be used by your code outside the package (customer end of the program need to import the package), which is known to export (such as object-oriented language in public); if the identifier begin with lowercase letters, then the outside of the package are not visible, but they are visible inside the whole package and available (like 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 the editor such as Sublime2, add the above code into the editor.

  2. Save the above codehello.go

  3. Open a command line and enter the program file stored in the directory.

  4. Enter the commandgo run hello.gopress Enter to execute code.

  5. If done correctly you will see"Hello World!"Output words on the screen.

$ go run hello.go
Hello, World!