Go语言学习日记【二十九】golang切片操作

package main

import "fmt"

func main() {
    initSlice := []int{1,2,3,4,5,6}
    copySlice := make([]int,6)
    copy(copySlice,initSlice)
    fmt.Println("-------copy slice:",copySlice)
    fmt.Println("-------------------------------1-------------------")
    tmpSlice1 :=  initSlice[:3]
    tmpSlice1[0] = 3
    fmt.Println("-------tmpSlice1:",tmpSlice1)
    fmt.Println("-------initSlice:",initSlice)
    fmt.Println("-------------------------------2-------------------")
    initSlice = []int{1,2,3,4,5,6}
    tmpSlice2 :=  initSlice[3:]
    tmpSlice2[0] = 1
    fmt.Println("-------tmpSlice2:",tmpSlice2)
    fmt.Println("-------initSlice:",initSlice)
    tmpSlice2 = append(tmpSlice2,7)
    fmt.Println("-------tmpSlice2:",tmpSlice2)
    fmt.Println("-------initSlice:",initSlice)
    fmt.Println("---------------tmpSlice2[0]:",tmpSlice2[0])
    tmpSlice2[0] = 2
    fmt.Println("-------tmpSlice2:",tmpSlice2)
    fmt.Println("-------initSlice:",initSlice)

    fmt.Println("-------------------------------3-------------------")
    initSlice = []int{1,2,3,4,5,6}
    tmpSlice3 :=  initSlice[3:4]
    tmpSlice3 = append(tmpSlice3,7)
    fmt.Println("-------tmpSlice3:",tmpSlice3)
    fmt.Println("-------initSlice:",initSlice)
    fmt.Println("---------------tmpSlice3[0]:",tmpSlice3[0])
    tmpSlice3[0] = 1
    fmt.Println("-------tmpSlice3:",tmpSlice3)
    fmt.Println("-------initSlice:",initSlice)

    delSlice := []int{1,2,3,4,5,6}
    del(delSlice,2)
    fmt.Println("----------delSice:",delSlice)

    addrDelSlice := []int{1,2,3,4,5,6}
    addrDel(&addrDelSlice,2)
    fmt.Println("----------addrDelSice:",addrDelSlice)

    fmt.Println("-------copy slice:",copySlice)
}

func del(s []int,a int) {
    if len(s) > a {
        s = append(s[:a],s[a+1:]...)
    }
    fmt.Println("------DEL FUNC:",s)
}


func addrDel(s *[]int,a int) {
    if len(*s) > a {
        *s = append((*s)[:a],(*s)[a+1:]...)
    }
    fmt.Println("------ADDR DEL FUNC:",*s)
}








/****************************************************
-------copy slice: [1 2 3 4 5 6]
-------------------------------1-------------------
-------tmpSlice1: [3 2 3]
-------initSlice: [3 2 3 4 5 6]
-------------------------------2-------------------
-------tmpSlice2: [1 5 6]
-------initSlice: [1 2 3 1 5 6]
-------tmpSlice2: [1 5 6 7]
-------initSlice: [1 2 3 1 5 6]
---------------tmpSlice2[0]: 1
-------tmpSlice2: [2 5 6 7]
-------initSlice: [1 2 3 1 5 6]
-------------------------------3-------------------
-------tmpSlice3: [4 7]
-------initSlice: [1 2 3 4 7 6]
---------------tmpSlice3[0]: 4
-------tmpSlice3: [1 7]
-------initSlice: [1 2 3 1 7 6]
------DEL FUNC: [1 2 4 5 6]
----------delSice: [1 2 4 5 6 6]
------ADDR DEL FUNC: [1 2 4 5 6]
----------addrDelSice: [1 2 4 5 6]
-------copy slice: [1 2 3 4 5 6]
*****************************************************/



参考博客:

Golang中切片的定义、使用_CodingPassion的博客-CSDN博客_golang切片定义

你可能感兴趣的:(Go语言学习日记,golang)