Go-Basic Knowledge 2-data type-Variable

Source: Internet
Author: User

1.0 variable meaning: a variable or variable refers to a number that can be changed without a fixed value. A variable is expressed as a non-numeric symbol. It is generally expressed as a Latin letter. Variables are the opposite of constants. Variables can be used to generalize instructions. If only real values can be used, the command can only be used in some cases. A variable can be used as a retainer for any value of a specific type.

2.0 expression of Go language Variables

2.1 usevarThe keyword is the most basic way for go to define variables. Unlike oC, go places the variable type behind the variable name:

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

2.2 define multiple variables

// Define the three variables var vname1, vname2, and vname3 type of "type ".

2.3 define variables and initialize values

// The variable for "variablename" Initialization is the value of "value" and the type is "type" Var variablename type = Value

2.4 initializing multiple variables at the same time

/* Define three variables of the "type" type, and initialize vname1 as V1, vname2 as V2, vname3 as V3 */var vname1, vname2, vname3 type = V1, V2, V3

2.5_(Underline) is a special variable name, and any value given to it will be discarded. In this example, we set the value35GrantbAnd discard them at the same time.34:

_, b := 34, 35

2.6 fast definition and initialization of private variables

/* Define three variables. They initialize vname1 as V1, vname2 as V2, and vname3 as V3. The Compiler automatically exports the corresponding types based on the initialized values. */vname1, vname2, vname3: = V1, V2, V3

2.7 private variables: The go language does not have the class concept, but uses the package form for encapsulation. Private variables are variables declared outside the function in the package.

Package mainimport "FMT" Var name string // after the data type declaration of the Go language, and the Go language is a strong type check, the same as swfit, cannot be implicitly converted to VaR, B intfunc main () {name = "today I am very happy, although I am not looking for a job, because of progress! We hope go can support Android development as soon as possible. We look forward to version 1.4! \ N "FMT. printf ("% s", name) C: = plus (1, 1) // local variable declaration method FMT. printf ("1 + 1 = % d", c)} // Add func plus (A int, B INT) int {
// The method is in the form of: Return A + B for the return value type of the method name (parameter type}

2.8 local variables: The go language does not have the class concept, but uses the package form for encapsulation. The local variables are the variables declared in the method and will be released after the scope is exceeded.

2.9 public variables: The go language does not have the class concept, but uses the package form for encapsulation. The global variables are the variables declared outside the method. They are capitalized and provide calls between external packages.

3.0 go reports an error during compilation for declared but unused variables

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.