Hello, go

Source: Internet
Author: User

Hello, go

The Go language is a compilation language similar to the C language, and its compilation speed is very fast. This language has a total of 25 keywords, one less than all English letters, which is very advantageous for our study. Let's take a look at what these keywords look like.

Break default func interface select

Case defer go map struct

Chan else goto package Switch

Const fallthrough if range type

Continue for Import Return VaR

In this chapter, I will guide you through the basics of the Go language. Through the introduction of each section, you will find that the world of Go language is so concise and the design is so wonderful, it will be a pleasure to write the go language. When you look back, you will find out how friendly these 25 keywords are.

Before writing an application, we start with the most basic program. Just as you don't know what the foundation is before building a house, you don't know how to start programming. Therefore, in this section, we will learn how to use the most basic syntax to run the go language program.

Program

This is like a tradition. Before learning most languages, you should first learn how to write a program that can output Hello world.

Are you ready? Let's get started!

Package main

 

Import "FMT"

 

Func main (){

FMT. printf ("Hello, world or hello, the world or the world where the Kappa λ is really ")

}

The output is as follows.

Hello, world or hello, the world is either or both.

Details

First, we need to understand a concept. The go language program is organized by package.

Package <pkgname> (package main in our example) tells us which package the current file belongs to, while package name main tells us that it is a package that can be run independently, it will generate executable files after compilation. Except for the main package, all other packages will generate *. A file (that is, the package file) is stored in $ gopath/PKG/$ GOOS _ $ goarch (take Mac as an example: $ gopath/PKG/darwin_amd64 ).

Each go language program that can be run independently must contain a package main. In this main package, it must contain an entry function main, which has neither parameters nor return values.

To print Hello, world..., we call a function printf, which comes from the FMT package, so we imported the FMT package at the system level in Row 3: Import "FMT ".

The concept of packages is the same as that of modules in Python. They all have some special advantages: modularization (the ability to divide programs into multiple modules) and reusability (each module can be used repeatedly by other applications ). Here, we only need to first understand the concept of the package, and we will compile our own package later.

In the fifth line, we define a main function through the keyword func, and the function body is put in {}, just as we usually write C, C ++, or Java. As you can see, the main function does not have any parameters. Next we will learn how to compile a function with parameters and return 0 or multiple values.

Row 6: Call the function printf defined in the FMT package. As you can see, this function is called through <pkgname>. <funcname>, which is very similar to Python.

As mentioned above, the package name and the folder Name of the package can be different. <pkgname> here is the package name declared through package <pkgname>, rather than the folder name.

Finally, we can see that the output contains many non-ASCII characters. In fact, the Go language is born to support UTF-8, and any character can be output directly, you can even use any character in the UTF-8 as an identifier.

Summary

The Go language uses package (similar to the python module) to organize code. The main. Main () function (this function is mainly located in the main package) is the entry point of every independent runable program. The Go language uses UTF-8 strings and identifiers (because the inventor of the UTF-8 is also the inventor of the Go language), so it is born with multilingual support.

 

Go language basics


Define Variables

There are multiple ways to define variables in the go language.

Using the VaR keyword is the most basic way to define variables in the go language. Unlike the C language, the Go language places the variable type behind the variable name, as shown below.

// Define a variable named "variablename" and of the type "type"

VaR variablename type

Defines multiple variables.

// Define three variables whose types are "type"

VaR vname1, vname2, vname3 type

Define variables and initialize values.

// The variable for initializing "variablename" is the value of "value" and the type is "type"

VaR variablename type = Value

Multiple variables are initialized at the same time.

/*

Define three types, which are all three variables of "type" and initialize the corresponding values respectively.

Vname1 is V1, vname2 is V2, and vname3 is v3

*/

VaR vname1, vname2, vname3 type = V1, V2, V3

Do you think the above definition is a little complicated? It doesn't matter, because the designers of the Go language have also discovered that there is a way to simplify it. We can directly ignore the type declaration, so the above Code becomes as follows.

/*

Define three variables and initialize corresponding values respectively.

Vname1 is V1, vname2 is V2, and vname3 is v3

Then go will help you initialize them based on their corresponding value types.

*/

VaR vname1, vname2, vname3 = V1, V2, V3

Do you think the above is complicated? Okay, let's continue to simplify it.

/*

Define three variables and initialize corresponding values respectively.

Vname1 is V1, vname2 is V2, and vname3 is v3

The compiler automatically exports the corresponding type based on the initialization value.

*/

Vname1, vname2, vname3: = V1, V2, V3

Does it seem very concise now? : = "Directly replaces VaR and type. This form is called a short statement. However, it has a limitation that it can only be used inside a function. If it is used outside a function, it cannot be compiled and passed. Therefore, global variables are generally defined using the VaR method.

_ (Offline) is a special variable name, and any value assigned to it will be discarded. In this example, we assign the value 35 to B and discard 34 at the same time.

_, B: = 34, 35

The Go language reports an error during compilation for declared but unused variables. For example, the following code produces an error: I is declared but not used.

Package main

 

Func main (){

VaR I int

}

Constant

The so-called constant, that is, the value determined during the program compilation phase, and the value cannot be changed during the program runtime. In a go program, constants can be defined as numerical values, Boolean values, or strings.

Its syntax is as follows.

Const constantname = Value

// You can specify the constant type if needed:

Const PI float32 = 3.1415926

The following are examples of constant declarations.

Const Pi = 3.1415926

Const I = 10000

Const maxthread = 10

Const prefix = "astaxie _"

This article is excerpted from go web programming.

Xie mengjun

Published by Electronic Industry Publishing House

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.