The constant and variable _golang in Golang programming

Source: Internet
Author: User
Tags constant

Go language constants
A constant is a fixed value that the program may not be able to change during its execution. These fixed values are also called literals.

Constants can be any basic data type such as an integer constant, a floating-point constant, a word constants, or a string literal. There are also enumerated constants.

Constants are the same, except that their values cannot be defined themselves to modify the general variable processing.

Integral type Constants
An integer literal can be decimal, octal, or hexadecimal constant. prefix specifies base or cardinality: 0x or 0X hexadecimal, 0 for octal, and not decimal.

An integer literal can also have a combination of a suffix of u and L, which is unsigned and long integer respectively. Suffixes can be either uppercase or lowercase, and can be in any order.

Here are some examples of integer constants:

Copy Code code as follows:

212/* Legal *
215u/* Legal * *
0xFeeL/* Legal * *
078/* Illegal:8 is not a octal digit * *
032UU/* Illegal:cannot repeat a suffix * *

The following are examples of different types of integer constants:
Copy Code code as follows:

* * * Decimal/
0213/* octal *
0X4B/* Hexadecimal *
/* int */
30U/* unsigned int */
30l/* long *
30ul/* unsigned long *

floating point Text (constant)
A floating-point literal has an integer part, a decimal point, a decimal part, and an exponential portion. You can represent decimal form or exponential form floating-point text.

Also in decimal form, you must include the decimal point, Index, or both and the exponential form, you must include the integer part, fractional part, or both. A signed exponent, expressed by E or E.

Here are some examples of floating-point value:

Copy Code code as follows:

3.14159/* Legal *
314159E-5L/* Legal *
510E/* illegal:incomplete exponent * *
210f/* Illegal:no decimal or exponent * *
. E55/* illegal:missing integer or fraction * *

escape sequence
There are some characters in go, preceded by a backslash they will have special meaning, they are used to represent similar line breaks (\ n) or tabs (\ t). Here, there are a list of such escape sequence codes:

The following examples illustrate some of the escape character sequences:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
Fmt. Printf ("hello\tworld!")
}


When the above code is compiled and executed, it produces the following results:
Copy Code code as follows:

Hello world!

string Literals
string literals or constants in double quotes "". A string contains characters similar to the word rune character: normal characters, escape sequences, and universal characters.

You can use strings and delimiters to break a long line into multiple lines using a space.

Here are some examples of strings. All three forms are the same string.

Copy Code code as follows:

"Hello, dear."

"Hello, \

Dear

"Hello," "D" "ear"


const keyword
You can use the const prefix to declare constants using specific types as follows:
Copy Code code as follows:

Const variable type = value;

The following example illustrates its details:
Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
const LENGTH INT = 10
const WIDTH INT = 5
var area int

Area = LENGTH * WIDTH
Fmt. Printf ("Value of area:%d", area)
}


When the above code is compiled and executed, it produces the following results:
Copy Code code as follows:

Value of AREA:50

Note that this is a good programming habit to capitalize on the definition of constants.

Go language variable
What the variable is, is not given to the storage area, our program can manipulate the name. Each variable in go has a specific type, it determines the size and the layout of the variable memory, can determine the range of values stored in the memory, and the group operation can be applied to the variable.

A variable name can consist of letters, numbers, and underscores. It must be marked with letters or underscores. Uppercase and lowercase letters are different, because go is case-sensitive. Based on the basic type, as explained in the previous section, the following basic variable types are available:

The Go programming language can also define various other types of variables, which we will list in later chapters, such as enumerations, pointers, arrays, structs, unions, and so on. For the cover of this chapter, let's just study the basic variable type of research.

In go, the variable definition:
A variable is defined to tell the compiler where, and how many, to create a storage variable. A variable definition specifies a data type and contains the type, such as the list of one or more of the following variables:

Copy Code code as follows:

var variable_list optional_data_type;

Here, Optional_data_type can include Byte, Integer, Float32,complex64, Boolean, or any user-defined object, such as valid go data types, variable_list can be separated by one or more identifier names. Some valid declarations are as follows:
Copy Code code as follows:

var I, j, K Int;
var c, CH-byte;
var f, salary float32;
D = 42;

This line of Var I, J, K; Both the variable i,j and k are declared and defined, which instructs the compiler to create an int type variable named I,j and K.

Variables can be initialized (assigning initial values) in their declarations. The type of the variable is automatically judged by the compiler based on the value passed to it. Initialization consists of an equal sign followed by a constant expression as follows:

Copy Code code as follows:

Variable_name = value;

Some examples are:
Copy Code code as follows:

D = 3, F = 5; Declaration of D and F. Here d and F are int

For uninitialized definitions: implicit 0 initialization with a static storage time variable (all bytes with a value of 0); The initial value of all other variables is the 0 value of their data type.

Static type declaration
A variable declaration of a static type is guaranteed to the compiler, and a variable exists with the given type and name, so that the compiler makes further edits without needing the full details of the variable. A variable declaration has its meaning at compile time, the compiler needs the actual variable declaration when linking the program.

Example
Try the following example where the variable has been declared to be typed and has been defined and initialized by the primary function:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
var x float64
x = 20.0
Fmt. PRINTLN (x)
Fmt. Printf ("x is of type%t\n", x)
}


Let's compile and run the above program, which will produce the following results:
Copy Code code as follows:

20
X is of type float64

dynamic type declaration/type inference
A dynamic type variable declaration requires that the compiler interprets a type that is passed to its value variable. The compiler does not need a variable static type that is necessarily required.

Example
Try the following example, where the variable has been declared without any type and has been determined to initialize in the main function. If the type is inferred, we have initialized the variable y using: = operator, x initialization using = operator.

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
var x float64 = 20.0

Y: = 42
Fmt. PRINTLN (x)
Fmt. Println (y)
Fmt. Printf ("x is of type%t\n", x)
Fmt. Printf ("Y is of type%t\n", y)
}


Let's compile and run the above program, which will produce the following results:
Copy Code code as follows:

20
42
X is of type float64
Y is of type int

Mixed Variable Declaration
Variables of different types can be one-step using type inference declarations.

Example

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
var A, B, C = 3, 4, "foo"

Fmt. Println (a)
Fmt. Println (b)
Fmt. Println (c)
Fmt. Printf ("A is of type%t\n", a)
Fmt. Printf ("B is of type%t\n", b)
Fmt. Printf ("C is of type%t\n", c)
}


Let's compile and run the above program, which will produce the following results:
Copy Code code as follows:

3
4
Foo
A is of type int
b is of type int
c is of type string

Left and right values in go:
There are two types of expressions in go:

Lvalue: An expression referencing a memory location is called a "left-value" expression. The left value may appear to the right of either left hand or assigned value.

Rvalue: The term right value refers to the data value of some address stored in the memory. The right value is a value that cannot be assigned to it, which means that the right value may appear on the right side of the assignment rather than the left expression.

Variables that are left values can appear on the left side of the assignment. Numeric text is a right value, so it may not be assigned and cannot appear on the left. The following is a valid statement:

Copy Code code as follows:

x = 20.0

However, the following is not a valid declaration and generates a compile-time error:
Copy Code code as follows:

10 = 20

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.