Go Language Basics

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

1. Variable declaration
The variable declaration of the go language differs significantly from the C and C + + languages. The go language introduces the keyword Var, and the go language puts the variable type behind the variable name;
1), declare a variable

    //Define a variable of type ' int '    varVNameint    //Define a variable of type "string"    varVName2string    //Define an array variable of type ' int ' with size 10    varVName3[Ten]int    //Define an array slice of type ' int '    varVName4 []int    //define a struct-body variable    varVName5struct{VNint}//Define a pointer variable of type ' int '    varVName6 *int    //Define a data dictionary, the key type is "string", the value type is "int"    varVName7Map[string]int

From the above example we can see that the variable declaration statement does not need to use a semicolon as a terminator, and declare it through the VAR keyword, while the go language to avoid the programmer need to repeat the keyword VAR, with the keyword VAR can put several variables need to be declared together, as follows:

var (    int    string)

2), declaring multiple variables of the same type

//定义类型都是"int"的三个变量varint

2. Variable initialization
The var keyword can be preserved, but is no longer the necessary element, as shown in the following example:

//定义一个类型为"int"的变量,并对其初始化值varint=1//定义一个变量,但未指定数据类型,并对其初始化值var1//定义一个变量,且对其直接初始化值vName :=1

Through the above three examples, you can see that the third usage is the most concise; Here the Go language introduces another character (a combination of colons and equal signs: =), which represents declaration and initialization.
While specifying that the type is no longer required, the go language compiler can push from the value of the initialization expression (to the right) to the type to which the variable should be declared.
For: = The variable on the left cannot be declared more than once, the go language is not allowed, as follows:

i :=1//这个是不允许的i :=2

Or

varint//这个是不允许的i :=1

Because the symbol: = indicates declaration and initialization, so is not allowed, after running the system will prompt: No new variables no left side of:=

Related Article

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.