• Tags: backend, Golang

Go variable

Variable, derived from mathematics, is an abstract concept in computer language that can store calculation results or represent values. We can access a variable by using a variable name, which, as mentioned earlier, can consist of letters, numbers, and underscore characters. It must begin with a letter or underscore.

A variable is simply the name of the storage area that the program can operate on. Each variable in Go has a specific type that determines the size and layout of the variable’s memory, the range of values that can be stored in that memory, and the set of operations that can be applied to the variable.

Definition of variables

We use the var keyword to declare variables:

var variable_list optional_data_type;
Copy the code

Optional_data_type in the code refers to valid Go data types (such as Boolean, byte, int, FLOAT32, complex64, etc.), and variable_list can contain one or more comma separated identifier names. Such as:

var  i, j, k int;
var  c, ch byte;
var  f, salary float32;
d =  42;
Copy the code

Pay attention to

I don’t know if you’ve noticed, but I’ve noticed that this definition differs from many languages

In the variable definition in Go, the type is opposite to the variable name!

Statement var I, j, k; The variables I, j, and k are declared and defined, telling the compiler to create I, j, and k variables of type int.

A variable can be initialized (assigned an initial value) in its declaration, and the type of the variable is automatically determined by the compiler based on the value passed to it.

Variable assignment and initialization

The initialization of a variable usually consists of an equal sign and a constant expression, as follows:

variable_name = value;
Copy the code

For instance:

penguin = 0
Penguin = 1
// Define two variables and their initial values
// Both variables here are 'int'
Copy the code

Brief definition

What? You’re saying it’s not easy? Go also gives us an easier way!

penguin := "Cute"
Copy the code

Pay attention to

We cannot use initialization declarations for variables with the same name again in the same scope — no new variables on left side of :=

But we can use penguin = “Great” to assign a new value to a variable.

If you use variable a before defining it, you get a compilation error undefined: a.

If you declare a local variable but do not use it in the same code block, you will also get a compilation error, as in the following example:

So let’s say nil

nil?

This……

Programmers should be familiar with the following code:

iferr ! =nil {
    / / do something...
}
Copy the code

First, let’s look at the definition of a dictionary

Initialization of a variable

In Go, if you declare a variable but do not assign to it, the variable will have a default zero value of type. This is the zero value for each type:

type The default value
bool false
numbers 0
string ""
pointers nil
slices nil
maps nil
channels nil
functions nil
interfaces nil

Oh, I don’t think there’s much to talk about

Go constants

A constant is an identifier of a simple value, an amount that will not be modified while the program is running. Data types in constants can only be bool, number (int, float), and string. Constants are defined as follows:

const identifier [type] = value
Copy the code

However, you can omit the type specifier because the compiler can infer the type of a variable from its value.

Alternatively, multiple declarations of the same type can be abbreviated as:

const c_name1, c_name2 = value1, value2
Copy the code

iota

Iota, a special constant, can be thought of as a constant that can be modified by the compiler. Under the const keyword, the occurrence of IOTA is reset to 0 (before the first line inside a const block), and ioTA is counted once for every new line declared in const.

Iota can be used as an enumeration value:

const (
    a = iota
    b = iota
    c = iota
)
Copy the code

The first IOTA is equal to 0, and then ioTA is automatically incremented by 1 each time it is used on a new row. It can be abbreviated as:

const (
    a = iota
    b
    c
)
Copy the code