Posted at

【メモ】Goで多次元Sliceのappend


Goで多次元Sliceのappend

1次元slice同士のappendと同じような感じでいける。


1次元Slice


コード

package main

import (
"fmt"
)

func main() {
s1 := []int{1, 2, 3, 4}
s2 := []int{5, 6, 7, 8}

s1 = append(s1, s2...)

fmt.Printf("%#v", s1)
}


実行結果

[]int{1, 2, 3, 4, 5, 6, 7, 8}


2次元Slice


コード

package main

import (
"fmt"
)

func main() {
s1 := [][]int{{1, 1}, {1, 2}, {1, 3}, {1, 4}}
s2 := [][]int{{2, 1}, {2, 2}, {2, 3}, {2, 4}}

s1 = append(s1, s2...)

fmt.Printf("%#v", s1)
}


実行結果

[][]int{[]int{1, 1}, []int{1, 2}, []int{1, 3}, []int{1, 4}, []int{2, 1}, []int{2, 2}, []int{2, 3}, []int{2, 4}}