Latest web development tutorials

Go linguistic variables

Variable from mathematics, computer languages ​​can be stored or calculated results can be represented value abstraction. Variables can be accessed via the variable name.

Go language variable names consist of letters, numbers, underscores, of which the first letter is not numeric.

The general form of a variable is declared using the var keyword:

var identifier type

Variable declaration

First, specify the variable type, the declaration if the assignment, use the default.

var v_name v_type
v_name = value

The second, based on the value of self-determination variable types.

var v_name = value

Third, omitting var, note: Variable = Left should not be declared in place, otherwise it will cause a compiler error.

v_name := value

// 例如
var a int = 10
var b = 10
c : = 10

Examples are as follows:

package main
var a = "w3cschool本教程"
var b string = "w3cschool.cc"
var c bool

func main(){
    println(a, b, c)
}

The above examples Implementation of the results:

w3cschool本教程 w3cschool.cc false

Multivariable declaration

//类型相同多个变量, 非全局变量
var vname1, vname2, vname3 type
vname1, vname2, vname3 = v1, v2, v3

var vname1, vname2, vname3 = v1, v2, v3 //和python很像,不需要显示声明类型,自动推断

vname1, vname2, vname3 := v1, v2, v3 //出现在:=左侧的变量不应该是已经被声明过的,否则会导致编译错误


// 这种因式分解关键字的写法一般用于声明全局变量
var (
    vname1 v_type1
    vname2 v_type2
)

Examples are as follows:

package main

var x, y int
var (  // 这种因式分解关键字的写法一般用于声明全局变量
    a int
    b bool
)

var c, d int = 1, 2
var e, f = 123, "hello"

//这种不带声明格式的只能在函数体中出现
//g, h := 123, "hello"

func main(){
    g, h := 123, "hello"
    println(x, y, a, b, c, d, e, f, g, h)
}

The above examples Implementation of the results:

0 0 0 false 1 2 123 hello 123 hello

Value types and reference types

All like int, float, bool and string these basic types are all value types, using these types of variables directly to the value stored in memory:

When you use the equal sign = when the value of a variable is assigned to another variable, such as: j = i , is in fact the value of memory i will be a copy of:

You can get the memory address of the variable i & i, for example: 0xf840000040 (each address may be different). Value stored variable value type on the stack.

Memory address will vary depending on the machine, or even the same program on different machines after the execution will have different memory addresses. Because each machine may have a different memory layout, and a position assignment may vary.

More complex data often requires the use of multiple words, these data are generally stored using a reference type.

A reference type variable r1 r1 is the value stored in the memory address where the (digital), or the position of the memory address of the first word is located.

The memory address is called a pointer that was actually there is another one word.

With reference to a plurality of types of word pointer may be in contiguous memory addresses (memory layout is continuous), which is a calculation of the most efficient form of storage; these can also be stored in memory words dispersed, each a word indicates where the next word memory address.

When the assignment statement r2 = r1, only references (address) is copied.

If r1 value is changed, then the value of all the citations will be modified to point, in this case, r2 also be affected.


Short form, use the: = assignment operator

We know that you can automatically infer the type of a variable is initialized by the system variable is omitted, the statement written on the declaration var keyword is actually a bit redundant, so we can be abbreviated them as a: = 50 or b: = false.

a and b types (int and bool) will be automatically deduced compiler.

This is the preferred form of the use of variables, but it can only be used in the body of the function, it can not be used to declare with the assignment to global variables. Use operators: = can efficiently create a new variable called initialization statement.

Precautions

If the same block of code, we can not re-use the same name for a variable initialization statement, for example: a: = 20 is not allowed, the compiler will display an error no new variables on left side of: =, but a = 20 is possible, because it is to the same variable is assigned a new value.

If you use it before you define the variable a, you will get a compile error undefined: a.

If you declare a local variable, but not used in the same block of code in it, you will get a compile error, such as the following example which variable a:

func main() {
   var a string = "abc"
   fmt.Println("hello, world")
}

Try to compile this code will get the errora declared and not used.

In addition, to give a simple assignment is not enough, this value must be used, so use

fmt.Println("hello, world", a)

It will remove errors.

But global variables are declared but not allowed to use.

>

Multiple variables of the same type can be declared on the same line, such as:

var a, b, c int

More variables can be assigned on the same line, such as:

a, b, c = 5, 7, "abc"

The above line assumes that the variables a, b and c have been declared, otherwise it should be used this way:

a, b, c := 5, 7, "abc"

These values ​​are on the right in the same order assigned to the variable on the left, so that a value is 5, b value is 7, the value of c is "abc".

This assignment is called parallel or simultaneously.

If you want to swap the values ​​of two variables, you can simply use a, b = b, a.

Blank identifier _ is also used discarded values, such as the value of 5: _, b = 5, 7 was abandoned.

_ Actually a write-only variable, you can not get its value. Go do this because the language in which you must use all the variables are declared, but sometimes you do not need to use all the return values ​​from a function obtained.

Parallel assignment is also used as a function return multiple return values, such as val and error err here is simultaneously obtained by calling Func1 function: val, err = Func1 (var1).