Initialization and assignment of "Go Language" "5" variables

Source: Internet
Author: User

In the real coding process to use a variable, you must declare before you can use, go language is no exception

1. Declaring variables

var postcode int    // Declares an integer variable postcode

var Phonenum int    //declares an integer variable Phonenum

var name string     //declares a string variable name

var address string  //declares a string variable address

Next we print directly in the main () method what each value is:650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/59/CB/wKiom1Tid_ Gggnbmaalfhnhvtbq020.jpg "title=" default value. png "alt=" wkiom1tid_gggnbmaalfhnhvtbq020.jpg "/>

As you can see from the above, although we have just declared a variable, GO helps us to automatically initialize a default value for these variables, where the default value of the integer is 0, and The default value of the string is empty

"Remarks":

Readers who have developed experience in other languages know that, like the above variable declaration is written in four lines, not concise, whether we can write this

var postcode, Phonenum int

var name, address string

Sure, you have to know that the go language is a good program for lazy programmers, yo:)


2. Initialization

If the default value of Go auto-initialization is not what we want, we can initialize the variable at the same time as the declaration.

var postcode int = 252039

var phonenum int = 13126718111

var name string = "Qingke"

var address string = "Shenzhen City, Guangdong province"

Print out the respective values:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/C8/wKioL1Tie8ShtRIJAAMICds65-c477.jpg "title=" Initialize. png "alt=" wkiol1tie8shtrijaamicds65-c477.jpg "/>

OK, as expected. Can we also simplify it? The answer is: There's a lot more to it than just simplifying.



Mystery 1: Remove the type behind the variable

var postcode = 252039

var phonenum = 13126718111

var name = "Qingke"

var address = "Shenzhen City, Guangdong province"

Ctrl+b, enter run a bit, see if there are errors
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/CA/wKioL1Tj3Uawn3ykAALj63aSCWg493.jpg "title=" Variable definition simplifies. png "alt=" wkiol1tj3uawn3ykaalj63ascwg493.jpg "/>

Go is very intimate, tell us this can also use


Mystery 2: Write on one line of the same type and assign values separately

var postcode, Phonenum = 252039, 13126718111

var name, address = "Qingke", "Shenzhen City, Guangdong province"

Ctrl+b, enter run a bit, see if there are errors

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/59/CA/wKioL1Tj3jOg-mThAAMguIPgXVE352.jpg "title=" Variable definition simplified 2.png "alt=" Wkiol1tj3jog-mthaamguipgxve352.jpg "/>go is still sweet.


Mystery 3: Direct removal of variable identifiers

Postcode, Phonenum : = 252039, 13126718111

Name, address : = "Qingke", "Shenzhen City, Guangdong province"

Ctrl+b, enter run a bit, see if there are errors

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/DA/wKiom1TsaQLwir0hAAL2_Z9ahQU046.jpg "title=" Variables are initialized directly. png "alt=" wkiom1tsaqlwir0haal2_z9ahqu046.jpg "/>

The reader may have noticed that the Var was removed at the same time = changed to: =


3. Assigning values to variables

In go syntax, variable initialization and variable assignment are two different concepts, the following is the assignment process after declaring a variable:

var name string

Name = "Qingke"

Example:

If both the variable A and the variable B are integral, where a has a value of 10,b of 12, and the write program swaps the values of A and B

var a, b int

A, B = ten, //variable assignment

Fmt. Println ("a =", A, "B =", b)

A, B = B, value Interchange for A//A and B

Fmt. Println ("a =", A, "B =", b)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/D6/wKioL1TsbUawESTYAAJihRzuqR4989.jpg "title=" Variable assignment. png "alt=" wkiol1tsbuawestyaajihrzuqr4989.jpg "/>

Readers of the first contact with go may find that the digital exchange did not introduce intermediate variables! Yes, go is so willful


4, Study questions

Think 1:

var a int //define variable a

A: =/ /define and initialize the variable a

What happens when you write this code in the same method body? In other words, using: = Does it mean that the variable is not defined?

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/D6/wKioL1Tsb4bybJ5-AAJmahBJlQM654.jpg "title=" Think 1.png "alt=" Wkiol1tsb4bybj5-aajmahbjlqm654.jpg "/>

From the running result, using: = variable definition and initialization at the same time, so that no new variables appear, here a naming conflict


Think 2:

var a int32 = + //define variable A to initialize simultaneously

B: = + //define variable B to initialize at the same time


Fmt. Println (A = = b) //think if printing is true or false?

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/D7/wKioL1TscO7wrXFDAAIn5R0YEa4307.jpg "title=" Think 2.png "alt=" Wkiol1tsco7wrxfdaain5r0yea4307.jpg "/>

The result of the operation tells us that int32 and int cannot be compared directly, although both A and B are integral types, but A is int32 and B is set by the Go language default to Int,go that int32 and int are two different types, it is also possible to see that go is a type-oriented

5. Homework

The default value of the integer is 0, the default value of the string is an empty string, which is the default value of the other types?

This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1615103

Initialization and assignment of "Go Language" "5" variables

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.