Go language Learning-variables

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

Note: Go language, it does not require a semicolon at the end of the statement, and python similar.

Note: Go in the language, it is not allowed to define unused variables, otherwise the error is like Import a useless one. Package will be error-like.

1, the pure variable declaration

For a purely variable declaration, you must start with the keyword Var, followed by a space followed by a defined variable name, followed by a space followed by a variable type. Such as:

var tmpint int

var tmpstring string

var Tmpintarray [10]int

var tmpslice []int//Array Slice

var tmpstruct struct {

Tmpi int

}

var tmpptr *int

VAR (

tmpint_1 int

Tmpstring_1string

)

2. Initialization and assignment of variables

The go language introduces a new assignment symbol ": =", followed by a colon, followed by an equal sign that defines the variable and initializes it directly. For example, the following 3 statements are equivalent:

Tmpnewvar: = 100

var Tmpnewvar int = 100

var Tmpnewvar = 100

Note: " := "This assignment symbol, the left must be a non-existent variable, if it is already existing variables, then compile an error." Because the symbol itself represents the "define variable" and "Initialize variable" 2 actions, it is necessary to define a new variable.

For variables already defined with VAR, you can assign values directly.

Note: Go when a language is not assigned a variable type, it can automatically determine the type of the variable to the left based on the type of the value to the right of the equal sign, similar to Python .

Multiple assignments:

Go languages support multiple assignments, similar Python , such as:

I, j = j, I

The above line of code can be completed I and the J interchange of values.

2. Anonymous variables

In the go language, you can support functions to return multiple return values, and if you want to take only a subset of them, you can use the underscore "_" to make placeholders (called "placeholders" that do not know whether it is appropriate or to write it yourself), and put them in the position of the return variable. Such as:

Import "FMT"

funcGetpara () (para1,para2,para3int) {

return1,4,7

}

Func Main () {

_, ret, _: =getpara ()

Fmt. Println ("ret=", ret)

}

In the main function above, you only want to get the PARA2 returned by Getpara, and you can place the placeholder in the 1th, 3 position with the underscore.

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.