Variables and constants of Go learning notes

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

1 Variables

1-1 statement

Declaring variables using the keyword var in the Go language

For example

var int_a int; In most cases, semicolons can be omitted

When there is no initialization value at the time of the Declaration, Go sets the default value for the underlying type.

int 0
int8 0
Int32 0
Int64 0
UINT 0x0
The actual type of Rune 0//rune is int32
The actual type of byte 0x0//byte is uint8
float32 0//Length 4 byte
Float64 0//Length 8 byte
BOOL False
String ""

Declarations can also be assigned initialization, such as

var int_a int =1

Declaring simultaneous assignments can be simply abbreviated to

Int_a: = 1

More than 1-2 declarations of the same type

Multiple declarations of the same type can be abbreviated as

var A, b, C int

You can also assign values at the same time

var A, b, c int = 1, 2, 3

can also be shortened to

A, B, c: = 1, 2, 3

More than 1-3 different types of declarations

Several different types of declarations can be abbreviated as

VAR (

a int

B string

)

You can also assign values at the same time

VAR (

a int = 1

b string = "abc"

)

1-4 Special Empty ID _

Any assigned null identifier _ will be discarded, generally used to make the return value has multiple values, but may only use a few of them, only the use of values, the other values can be discarded.

A, _, c: = 1, 2, 3

The value 2 will be discarded.

2 Constants

2-1 statement

Declaring a variable in the Go language uses the keyword const, and the value of a constant can only be one of the bool, string, or numeric types.

For example

const a int = 1

Because the declaration constants must be initialized, the type can be omitted, i.e.

Const A = 1

More than 2-2 declarations of the same type

Multiple declarations of the same type can be abbreviated as

Const A, b = 1, 2

2-More than 3 different types of declarations

Several different types of declarations can be abbreviated as

Const (

A = 1

b = "ABC"

c = False

)

2-4 Special Keyword Iota

Go has a keyword iota, which is used when declaring an enum, with a default starting value of 0, plus 1 for each call

Const (

A = iota//a = 0

b//b = 1

c//C = 2

)

Iota can also participate in operations, such as

Const (
A = 1 << iota//A = 1 (Iota has been reset) 1*2^0
b = 1 << iota//b = 2 1*2^1
c = 1 << iota//c = 4 1*2^2
)

Resources

"Go language. Cloud Power"

"Go Web Programming"

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.