Golang Study notes (3)---definition and assignment of Go language variables, constants

Source: Internet
Author: User

Assignment and declaration of a single variable

Declaration format for variables: var < variable name > < variable type >

Variable assignment format:< variable name > = < expression >

Simultaneous assignment of declarations: var < variable name > [Variable Type] = < expression >

< variable name >: = < Variable value >---> can only be used within the function body

Replication and declaration of multiple variables

The declaration of a global variable can be shortened by using VAR ()

The declaration of a global variable can not omit Var, but can be used in a parallel way

All variables can use type inference

Declaration of parallel Mode:

var a,b,c,d int=1,2,3,4

function Body:

A,b,c,d: =1,2,3,4

whitespace characters

_ Use whitespace characters to ignore a return value

Type conversions for variables

There is no implicit conversion in go, all type conversions must be explicitly declared--ensuring that go is a type-safe language

Conversions can only occur between two mutually compatible types

Format: < variable a> [:] Type variable A (variable b)

var a float32 = 1.1b: = Int (a) because the conversion of a floating-point type to an integer is just a loss of precision, this can be converted to var a bool = Trueb: = Int (a) the expression cannot be compiled because the type is incompatible, so it cannot be converted to each other

Note: string (), when converting int to string, means that variable A is converted to string format because anything stored in the computer is essentially a number represented by 0 and 1, so this function naturally thinks we need the text represented by the number 65 (A).

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/86/E7/wKiom1fOYljzDVENAACHTptK-d0595.png-wh_500x0-wm_3 -wmp_4-s_1888920810.png "title=" 1.png "style=" Float:none; alt= "Wkiom1foyljzdvenaachtptk-d0595.png-wh_50"/>

So if we're going to convert 65 to the string "65", we need to use the StrConv package. (StrConv. ITOA)

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/86/E7/wKiom1fOYlvjyIGSAADEKYqM6yU554.png-wh_500x0-wm_3 -wmp_4-s_1700972788.png "style=" Float:none; "title=" 2.png "alt=" Wkiom1foylvjyigsaadekyqm6yu554.png-wh_50 "/>

And if you convert the string to int, then you need to use (StrConv. Atoi)

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/86/E6/wKioL1fOYl2wTFIPAAC517D6q0s399.png-wh_500x0-wm_3 -wmp_4-s_1061036445.png "title=" 3.png "style=" Float:none; alt= "Wkiol1foyl2wtfipaac517d6q0s399.png-wh_50"/>

Note: You can only convert string numbers, like "1", "123" and so on.

Definition of constants

-Solid value is determined at compile time

-constants are defined in the same format as variables

-the Equals right must be a constant or constant expression

-the function of a constant expression must be a built-in function

Initializing rules and enumerations

-When defining a constant group, if you do not provide an initial value, the expression using the previous row (note that if two constants are defined on the previous line, the next line also defines two constants)

-Using the same expression does not mean that you have the same value

-Iota is a constant counter, starting with 0, each of the 1 constants in a group is automatically incremented by 1

-enumeration effects can be achieved by initializing rules and iota

-Iota resets to 0 for every const keyword encountered

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/86/E7/wKiom1fOYl7iDsAuAAA2iCu6_cs895.png-wh_500x0-wm_3 -wmp_4-s_3924486609.png "title=" 4.png "style=" Float:none; alt= "Wkiom1foyl7idsauaaa2icu6_cs895.png-wh_50"/>

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/86/E6/wKioL1fOYmDwhjp1AACQjF8jn8o623.png-wh_500x0-wm_3 -wmp_4-s_2128466830.png "title=" 5.png "style=" Float:none; alt= "Wkiol1foymdwhjp1aacqjf8jn8o623.png-wh_50"/>

Since two constants (0,1) have already been defined, the defined C is 2 if iota, until the next const is reset to 0.

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/86/E7/wKiom1fOYmKTnYeCAACQ0ugUJb8834.png-wh_500x0-wm_3 -wmp_4-s_2041181271.png "title=" 6.png "style=" Float:none; alt= "Wkiom1foymktnyecaacq0ugujb8834.png-wh_50"/>

Constant naming recommendations: use uppercase for recommendations. If you do not want the external reference to be preceded by the addition of C (const) or _ to differentiate

This article is from the "Your Night" blog, be sure to keep this source http://lixin15.blog.51cto.com/3845983/1846837

Golang Study notes (3)---definition and assignment of Go language variables, constants

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.