Go study notes - array, slice

Go study notes - array, slice


Thanks for watching, if you have any questions, please leave a message to correct me! Thanks


1. Array

one-dimensional array

In the Go language, an array is a fixed-length sequence of numbers.

Methods for creating arrays and array initialization:

  • var a [5]int  //创建定长的数组,同时初始化默认值全为0
    
  • b := [5]int{
          
          1,2,3,4,5}  //自动推导法,在创建数组的同时进行初始化
    
  • var c = [...]int{
          
          1,23,5,623,7}  //不定长数组,再定义数组的同时进行初始化
    

Two-dimensional array

  • //二维数组的定义和初始化
    a := [3][2]string{
          
          
        {
          
          "安徽", "芜湖"},
    	{
          
          "江苏", "无锡"},
    	{
          
          "河北", "保定"},
    }
    

The length of the array can be len()obtained using the built-in function

The storage type of an array is single, but you can combine these data to construct a multi-bit data structure.

The use of arrays can be fully understood through the following examples:

func main(){
    
    
	var a [5]int
	fmt.Println("emp:",a)

	a[4] = 100
	fmt.Println("set:",a)
	fmt.Println("get:",a[4])

	fmt.Println("len:",len(a))

	b := [5]int{
    
    1,2,3,4,5}
	fmt.Println("dcl:",b)

	var twoD [2][3]int
	for i:=0;i<2;i++{
    
    
		for j:=0;j<3;j++{
    
    
			twoD[i][j] = i+j
		}
	}
	fmt.Println("2d:",twoD)
}

2. slice

Slice is a key data type in Go, and it is a more powerful sequence interface than array.

The type of slice is only determined by the elements it contains, and the definition does not require the number of elements.

Methods for creating slices and slice initialization:

  • var sli1 []int  //定义一个整型切片,默认为空
    
  • var sli2 = []string{
          
          }  //定义一个字符串切片,虽然没有赋值,但是切片不为空
    
  • sli3 := []int{
          
          }  //自动推导类型,切片不为空
    
  • sli4 := make([]int,4)  //make([]type,len,cap),使用内置函数创建,需要添加[]type,len或者cap,切片不为空
    

    Verify by code:

    func main(){
          
          
    	var sl1 []string
    	if sl1 == nil{
          
          
    		fmt.Println("空切片")
    	}else{
          
          
    		fmt.Println("不是空切片")
    	}
    
    	var sli2 = []int{
          
          }
    	if sli2 == nil{
          
          
    		fmt.Println("空切片")
    	}else{
          
          
    		fmt.Println("不是空切片")
    	}
    
    	sli3 := []int{
          
          }
    	if sli3 == nil {
          
          
    		fmt.Println("空切片")
    	}else{
          
          
    		fmt.Println("不是空切片")
    	}
    }
    
    //空切片
    //不是空切片
    //不是空切片
    

len()function

A built-in function len()to find the length of a slice.

func len(v Type) int
//Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
//返回切片中元素的个数,如果切片为空,返回0

cap()函数

A built-in function cap()that can find the capacity of a slice.

func cap(v Type) int
//Slice: the maximum length the slice can reach when resliced;
//重组切片时,切片可以达到的最大长度

Test code:

func lenAndCap(t []string) {
    
    
	//获取长度,内置函数len()
	fmt.Println("len:",len(t))
	//获取容量,内置函数cap()
	fmt.Println("cap:",cap(t))
}

append()function

A built-in function append()that adds elements to an already created slice.

func append(slice []Type, elems ...Type) []Type
//第一个参数是切片,第二个参数是添加的元素...元素可以有多个,返回值是切片类型
//也可以将后一个切片添加到前一个切片中
//numsSorted := append([]int(nil),nums...)

copy()function

A built-in function copy()that can copy elements to an already created slice.

func copy(dst, src []Type) int
//将原切片中的元素添加到目标切片

Test code:

func main(){
    
    
	s = append(s,"d")
	s = append(s,"e","f")
    fmt.Println("sel:",c)
	//lenAndCap(s)

	c := make([]string,len(s))
	copy(c,s)    //拷贝,将参数2中的元素拷贝到参数1中
	fmt.Println("cpy:",c)
	lenAndCap(c)
}

//sel: [a b c d e f]
//cpy: [a b c d e f]

Slices can form multidimensional data structures. The internal slice lengths can vary.

//构建二维切片
func main(){
    
    
    twoD := make([][]int,3)
    for i := 0;i < 3;i++{
    
    
        innerLen := i+1
        twoD[i] = make([]int,innerLen)
        for j := 0;j < innerLen;j++{
    
    
            twoD[i][j] = i+j
        }
    }
    fmt.Println("2d:",twoD)
}

Guess you like

Origin blog.csdn.net/weixin_46435420/article/details/119427260