go言語のスライス(slice)をしょっちゅう忘れるので、メモ

package main

import "fmt"

func main() {
	var s []int
	printSlice(s)

	// append works on nil slices.
	s = append(s, 0)
	printSlice(s)

	// The slice grows as needed.
	s = append(s, 1)
	printSlice(s)

	// We can add more than one element at a time.
	s = append(s, 2, 3, 4)
	printSlice(s)
}

func printSlice(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

あとappendの取扱については、これなど

// 後半部の処理
					sliceX1 = sliceZ[0]
					base_a = sliceZ[0][i]

					sliceX2 = append(sliceX1[:i], sliceX1[i+1:]...) // i番目の要素を削除

					//fmt.Println("A:sliceX2:", sliceX2[0:10], sliceX2[10:20], sliceX2[20:30], sliceX2[30:90])

					sliceX2 = append(sliceX2[:31], sliceX2[30:]...) // i番目の要素を先頭に挿入
					//fmt.Println("B:sliceX2:", sliceX2[0:10], sliceX2[10:20], sliceX2[20:30], sliceX2[30:90])

					sliceX2[30] = base_a
					//fmt.Println("C:sliceX2:", sliceX2[0:10], sliceX2[10:20], sliceX2[20:30], sliceX2[30:90])

					sliceZ[0] = sliceX2

					//fmt.Println("D:", sliceZ[0][0:10], sliceZ[0][10:20], sliceZ[0][20:30], sliceZ[0][30:90])
					//fmt.Println()

未分類

Posted by ebata