What are the methods of variable declaration in go language?

青灯夜游
Release: 2023-01-12 10:32:33
Original
3092 people have browsed it

Variable declaration methods are: 1. Use var to declare a variable and indicate the data type of the variable. The syntax is "var variable name type = expression"; 2. Use the ":=" character to make a short variable declaration. , the syntax "variable name:= expression". When using short variable declaration, you can omit the var keyword and type information. The default type of the variable is bool, rune, int, float64, complex128 or string.

What are the methods of variable declaration in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

An important feature of learning static languages ​​is different from dynamic languages: variable declaration.

How does the compiler or interpreter of a programming language know the boundaries of the memory area that a variable can reference?

Dynamic languages ​​and static languages ​​have different processing methods:

  • The interpreter of dynamic languages ​​(such as Python, Ruby, etc.) can assign values ​​to variables at runtime. Analysis, automatically determines the boundaries of variables. And in dynamic languages, a variable can be assigned bounds of different sizes at runtime.

  • The static programming language has a "slightly worse experience" in this regard. The statically typed language compiler must clearly know the boundaries of a variable before it is allowed to use this variable, but the static language compiler does not have the ability to automatically provide this information. This boundary information must be provided by the user of the language, so there is a "variable" statement". Through variable declaration, language users can explicitly tell the compiler the boundary information of a variable. At the specific implementation level, this boundary information is given by the type attribute of the variable.

As the Go language in the static programming language camp, it follows this requirement of static languages: variables need to be declared before using them.

[golang] Variable declaration and initialization var, :=, new() and make()

Go language provides a variety of variable declaration and initialization methods method. Here we focus on explaining them one by one. And provide a simple guide.

Guide

  • Use make() to initialize slice,map and channel.
  • In most cases, when the type is clear, use short variable declarationmethod:=.
  • When using text to initialize a variable and the type needs to be specified, use the varvariable declaration method.
  • Avoid using new() unless you need a pointer variable.

Variable declaration method

Go language can use var to declare a variable and specify the variable's type of data.

	// 初始化整数变量,值为10。
	var v int = 10
	fmt.Println(v)
	// 输出: 10

	// 变量声明: 一个slice变量
	var vSlice []int = []int{1, 2, 3, 4}
	fmt.Println(vSlice, "type: ", reflect.TypeOf(vSlice).Kind())
	// 输出: [1 2 3 4] type: slice

	// 短变量声明: 一个map变量,指向的值为[]
	var vMap map[string]int = map[string]int{
		"a": 1,
		"b": 2,
	}
	fmt.Println(vMap)
	// 输出: map[a:1 b:2]
Copy after login

Short variable declarations

short variable declarations Symbol: :=.

When a short variable is declared, the default type of the variable is: bool, rune, int, float64, complex128 or string

	// 短变量声明: 一个整数变量。
	sdvInt := 10
	fmt.Println(sdvInt, "type: ", reflect.TypeOf(sdvInt).Kind())
	// 输出: 10 type:  int

	// 短变量声明: 一个slice变量
	sdvSlice := []int{1, 2, 3, 4}
	fmt.Println(sdvSlice, "type: ", reflect.TypeOf(sdvSlice).Kind())
	// 输出: [1 2 3 4] type: slice

	// 短变量声明: 一个map变量,指向的值为[]
	sdvMap := map[string]int{
		"a": 1,
		"b": 2,
	}
	fmt.Println(sdvMap)
	// 输出: map[a:1 b:2]
Copy after login

new(T)

##new(T) Features:

    Allocate memory according to type T
  • Set the memory to 0
  • Return the memory pointer
  • 	// 初始化一个整数指针变量,指向的值为0
    	var i3 *int = new(int)
    	fmt.Println(*i3)
    
    	// 初始化一个slice指针变量
    	var i4 = new([10]int)[0:5]
    	fmt.Println(i4, "type: ", reflect.TypeOf(i4).Kind())
    	// 输出: [0 0 0 0 0] type: slice
    
    	// 初始化一个map指针变量,指向的值为[]
    	var i5 *map[string]int = new(map[string]int)
    	fmt.Println(*i5)
    	// 输出: map[]
    
    	// 初始化一个chan指针变量,指向的值为nil
    	var i6 *chan int = new(chan int)
    	fmt.Println(*i6)
    	// 输出: nil
    Copy after login

make()

make is only used to initialize

slice, map and channel.

	// make只能用于创建slice, map, channel
	// 切片类型(slice)
	makeSlice := make([]int, 5, 10)
	fmt.Println(makeSlice)
	// 输出: [0 0 0 0 0]

	// Map 类型
	var makeMap map[string]int = make(map[string]int)
	fmt.Println(makeMap)
	// 输出: map[]

	// Channel 类型
	var makeChan chan int32 = make(chan int32, 100)
	fmt.Println(makeChan)
	// 输出: 0xc000112000
Copy after login

Complete source code

package main

import (
	"fmt"
	"reflect"
)

func main() {

	// 初始化整数变量,值为10。
	var v int = 10
	fmt.Println(v)
	// 输出: 10

	// 变量声明: 一个slice变量
	var vSlice []int = []int{1, 2, 3, 4}
	fmt.Println(vSlice, "type: ", reflect.TypeOf(vSlice).Kind())
	// 输出: [1 2 3 4] type: slice

	// 短变量声明: 一个map变量,指向的值为[]
	var vMap map[string]int = map[string]int{
		"a": 1,
		"b": 2,
	}
	fmt.Println(vMap)
	// 输出: map[a:1 b:2]

	// 短变量声明: 一个整数变量。
	sdvInt := 10
	fmt.Println(sdvInt, "type: ", reflect.TypeOf(sdvInt).Kind())
	// 输出: 10 type:  int

	// 短变量声明: 一个slice变量
	sdvSlice := []int{1, 2, 3, 4}
	fmt.Println(sdvSlice, "type: ", reflect.TypeOf(sdvSlice).Kind())
	// 输出: [1 2 3 4] type: slice

	// 短变量声明: 一个map变量,指向的值为[]
	sdvMap := map[string]int{
		"a": 1,
		"b": 2,
	}
	fmt.Println(sdvMap)
	// 输出: map[a:1 b:2]

	// 初始化一个整数指针变量,指向的值为0
	var newInt *int = new(int)
	fmt.Println(*newInt)

	// 初始化一个slice指针变量
	var newSlice = new([10]int)[0:5]
	fmt.Println(newSlice, "type: ", reflect.TypeOf(newSlice).Kind())
	// 输出: [0 0 0 0 0] type: slice

	// 初始化一个map指针变量,指向的值为[]
	var newMap *map[string]int = new(map[string]int)
	fmt.Println(*newMap)
	// 输出: map[]

	// 初始化一个chan指针变量,指向的值为nil
	var newChan *chan int = new(chan int)
	fmt.Println(*newChan)
	// 输出: nil

	// make只能用于创建slice, map, channel
	// 切片类型(slice)
	makeSlice := make([]int, 5, 10)
	fmt.Println(makeSlice)
	// 输出: [0 0 0 0 0]

	// Map 类型
	var makeMap map[string]int = make(map[string]int)
	fmt.Println(makeMap)
	// 输出: map[]

	// Channel 类型
	var makeChan chan int32 = make(chan int32, 100)
	fmt.Println(makeChan)
	// 输出: 0xc000112000

}
Copy after login
[Related recommendations:

Go video tutorial, Programming teaching

The above is the detailed content of What are the methods of variable declaration in go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template