Go language variables created in five ways

For only the Python language experience of a friend, may not understand the statement of the word, in Python directly used to use, they do not declare what type of.

Go language is statically typed language, due compile time, the compiler checks the type of a variable, it requires that all variables must have a clear type.

Before using variables, we need to declare. Declared type, it is agreed that you can only assign a value to a variable of that type.

There are four ways to declare general, the first two of which also can be used to define constants, keywords simply varbecome constcan be.

The first : a row to declare a variable

var <name> <type>

In which the var keyword (fixed), name is the variable name, type is the type.

Var used, although only the specified type, but implicitly Go will be initialized, such as string type is initialized to an empty string, int type is initialized to 0, float initialized to 0.0, bool type is initialized to false, a pointer type initialized to nil.

If you want to process in a statement, by the way also initialized, you can write this

var name sting = "Python编程时光"

Go complete code in the file are as follows, in order not to write repetitive code, complete follow-up is no longer attached to the code, just paste the key code

package main

import "fmt"

func main()  {
	var name string = "Python编程时光"
	fmt.Println(name)
}

The value of the right (right-hand side value, rvalue) point of view, is obviously a string type (here to be noted that, in single quotes and double quotes Python equivalent, but in Go double and single quotation marks are not the same, here to be sure to use double quotes indicate a string, in single quotes indicate rune types of characters, the subsequent description will be separately), and therefore may be reduced to

var name = "Python编程时光"

If your right values ​​with a decimal point, in the case do not specify a type, the compiler will put your variable is declared as float64, but in many cases, we do not need such a high precision (larger memory space occupied)

In this case, it is recommended to specify the type, do not be lazy

var rate float32 0.89

The second : declare multiple variables together

Declare multiple variables, in addition to a plurality of rows can be written in accordance with the above, but also can be written as follows

var (
	name string
	age int
	gender string
)

Third : declare and initialize a variable

Use :=(or a short written statement derivation method type declaration: The compiler will automatically infer the value of the right type of the corresponding type of value left), can declare a variable, and (explicit) to initialize them.

name := "Python编程时光"

// 等价于

var name string = "Python编程时光"

// 等价于

var name = "Python编程时光"

However, this method has limitations that can only be used for internal functions

The fourth : declare and initialize multiple variables

name, age := "wangbm", 28

This method is often used to exchange variables

var a int = 100
var b int = 200
b, a = a, b

Fifth : new function to declare a pointer variable

Here to talk about the first pointer relevant content.

Two types of variables 普通变量and指针变量

Common variable, the data itself is stored, and a pointer is stored in the variable address data.

The following code, a common variable is age, content 28 is stored, and the value of variable age ptr is located in memory address: 0xc000010098

package main

import "fmt"

func main()  {
	var age int = 28
	var ptr = &age  // &后面接变量名,表示取出该变量的内存地址
	fmt.Println("age: ", age)
	fmt.Println("ptr: ", ptr)
}

Export

age:  28
ptr:  0xc000010098

And here say new function is a built-in function in Go.

Use the expression new (Type) will create a Type type of anonymous variable is initialized to zero value of type Type, and then return to the variable address pointer return type is *Type.

package main

import "fmt"

func main()  {
	ptr := new(int)
	fmt.Println("ptr address: ", ptr)
	fmt.Println("ptr value: ", *ptr)  // * 后面接指针变量,表示从内存地址中取出值
}

Export

ptr address:  0xc000010098
ptr value:  0

Creating variables and common way to create variable variable declaration statement is no different with the new, in addition to not need to declare a temporary variable name, we can also use the new (Type) in the expression. In other words, similar to the new function is a syntactic sugar, rather than a new foundation concept.

Follows two way, it can be said to be equivalent

// 使用 new
func newInt() *int {
    return new(int)
}

// 使用传统的方式
func newInt() *int {
    var dummy int
    return &dummy
}

Regardless of the above methods, variables / constants can be declared only once, repeatedly declared, the compiler will throw an error.

But there are exceptions, which comes to a particular variable: anonymous variable , also known as a placeholder, or blank identifier, underlined.

Anonymous variable, there are three advantages:

  • Not allocate memory, do not take up memory space
  • You do not need to name names and variables useless tangle
  • Repeated statements will not have any problems

Usually we must accept anonymous reception, but it will not be used value.

func GetData() (int, int) {
    return 100, 200
}
func main(){
    a, _ := GetData()
    _, b := GetData()
    fmt.Println(a, b)
}

Guess you like

Origin www.cnblogs.com/wongbingming/p/12570895.html