[Go]-variable declaration and definition

Source: Internet
Author: User

Email:Hahayacoder@gmail.com

 

1 goVariable description andJavascriptVery similar, useVaRKeywords

 

2 goThere are several forms of variable declaration and definition in 

//Declare and initialize a variable

VaRMInt=10

 

//Declare and initialize multiple variables

VaRI, J, K =1,2,3

 

//Declaration of multiple variables (Note the use of parentheses)

VaR(

NoInt

NameString

)

 

//The type is not specified during declaration. It is derived by the initialization value.

VaRB = true// BoolType

 

//A Simple MethodEquivalentVaRStrString="Hello"

STR: ="Hello"

 

 
// GoThere is a special Variable _Any value assigned to it will be discarded
 
_, RET: =2,3

 

3GoThe compiler reports an error for variables declared but not used.

 

4AndCThe same language,GoThe language also uses semicolons to terminate the statement. HoweverCDifferent Languages,GoThe Lexical analyzer of the language is being scanned.Source codeUse simple rules to automatically insert semicolons.CodeIn most cases, no extra points are required.

 

5 goRules for inserting semicolons in the language lexical analyzer:If the last mark in front of a new line is an identifier (includingIntAndFloat64Such a word), a basic text such as a value, or one of the following marks, a semicolon is automatically inserted.

 

6UsuallyGoThe language is onlyForUse semicolons to separate the initiaters, add, and increment. Another case is that when you write multiple statements in a row, you also need to use semicolons to separate them.

 

7BecauseGoThe special feature of adding semicolons to the language lexical analyzer.: You should not put a control structure(If,For,SwitchOrSelect. If this is done, a semicolon will be inserted in front of the braces, which may lead to unwanted results.

 

8A learning example

// Var Project main. gopackage mainimport ("FMT") func main () {// declare and initialize a variable var m Int = 10fmt. printf ("defined variable value: % d \ n", m) // declare to initialize multiple variables var I, j, k = 1, 2, 3fmt. printf ("defined variable values: % d, % d, % d \ n", I, j, k) // The type is not specified during the declaration, using the initialization value to derive var B = true // bool type if B = true {FMT. printf ("bool value is true. \ n ")} // a simple method is equivalent to VaR STR string =" hello "str: =" hello "FMT. printf ("defined variable value: % s \ n", STR) // The go compiler reports an error when declaring unused variables. // use the reserved word VaR to declare the variables and assign the VaR number intnumber = 121fmt to the variables. printf ("defined variable value: % d \ n", number) // another form of variable definition in this case, the variable type is derived from the value text: = "hahaya" FMT. printf ("defined variable value: % s \ n", text) // declaration of multiple variables (note the use of parentheses) var (no intname string) no = 1 name = "hahaya" FMT. printf ("student ID: % d \ t name: % s \ n", no, name) // another form of variable declaration and definition X, Y: = 2, "tosmile" FMT. printf ("student ID: % d \ t name: % s \ n", x, y) // go has a special Variable _ any value assigned to it will be discarded _, RET: = 2, 3fmt. printf ("variable value: % d \ n", RET )}

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.