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

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

Assignment and declaration of a single variable

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

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

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

< variable name >: = < Variable value > Listen---> 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 listen a,b,c,d listen to int=1,2,3,4

function Body:

A,b,c,d Listen: =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 listen-to-ensure that go is a type-safe language

Listening conversions can only occur between two mutually compatible types.

Listen to the format: < variable a> [:] Listen to type variable A (variable b)

var listen to float32 listen = Listen to 1.1b listen: = Listen int (a) because the conversion of floating-point type to integer is just a loss of precision, this is can be converted var listen = listen to Trueb listen: = Listen int (a) the expression cannot be compiled because the type is incompatible, So we can't convert 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).

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


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

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

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

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

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.