Learning Golang Language (3)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Variable Declaration
The go language differs from other languages in that the type of the variable is after the variable name. For example:
c: int A; in go: var a int.
In addition, if the go language does not assign a value when defining a variable, the variable is assigned a value of 0 of its type.
There are two ways to declare a variable: Use the var keyword, or use the: = operator to assign a value.
For example:
var number int//declares variable number is int data, at which time the default copy is 0
Number: = 2//directly declares the variable # and assigns a value of 2

Attention
Using the: = operator to declare a variable and its initial value, you do not have to explicitly indicate the variable type, because the initial value already illustrates the type of the variable.
: the = operator completes the operation of the variable declaration and assignment at the same time.
Using the: = operator, for the shaping of the literal go language infers that its type is int, for floating-point literals the go language infers that its type is float64 for the plural literal go language to infer its type complex128.
The usual practice is not to explicitly declare its type unless we need to use a special type that the go language cannot infer.


Multiple var declarations can be grouped, and const and import can do similar things. using parenthesesFor example:
var
a int
b BOOL
)


Multiple variables of the same type can be declared on one line:
var x, y int//The same can be used when declaring an int variable with an X. = operator
B: = 100,101//Assign A and b as int variables, respectively: 100 and 101

There is also a special variable name ' _ ' (underscore) in go, which means that all values assigned to it are discarded.
_, B: = 100, 101//means discard 100, assign 101 to variable b


for variables that have been declared but not used, the compiler will make an error at compile time. This embodies the idea of the Go language introduction: All the useless code, the library should not appear.


Constants

When it comes to variable declarations, it's definitely necessary to mention the exceptions in variables: constants.
Constants in Go are also constant. It is created at compile time and can only be a number, string, or Boolean value.
Constant declaration format: const identifier [Type] = value; For example: const PI = 3.14159 generates x this constant.
Where [type] is optional. The compiler automatically chooses the type for the constant based on the type of the value being assigned.
For example:
Const a string = "Hello"
Const A = "Hello"
The above two effects are the same.


constants must be assigned at compile time, constants can be defined as a formula, but all values in the formula are determined at compile time.
For example:
Correct: const C1 = 2/3
Error: Const C2 = GetNumber ()//Compile error because GetNumber () is not determined at compile time.


Numeric constants do not require the size and symbol, can be arbitrarily high precision, will not overflow.
Const ln2= 0.693147180559945309417232121458\
176568075500134360255254120680009
Const log2e= 1/LN2//This is a precise reciprocal
CONST BILLION = 1e9//float constant
Const Hardeight = (1 <<) >> 97


Numeric constants overflow Only when an assignment is given to a variable whose precision is too low to represent its value. This will cause an error at compile time.
Multiple constant declarations:
Const beef, both, c = "Meat", 2, "veg"
Const Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = 1, 2, 3, 4, 5, 6
Const (
Monday, Tuesday, Wednesday = 1, 2, 3
Thursday, Friday, Saturday = 4, 5, 6
)


You can use the keyword when generating enumeration values: Itoa.
Const
A = Itoa
b = Itoa
)

The first itoa represents 0, so a=0, when Iota is used again in a new row, its value increases by 1. So b =1.
In addition, you can omit it for a moment:
Const
A = Itoa
B
)
For example: In the Time Library: the name of the day of the week:
Const (
Sunday = Iota//0
Monday//1
Tuesday//2
Wednesday//3
Thursday//4
Friday//5
Saturday//6
)

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.