0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

多次元スライスにスライスを挿入する

Last updated at Posted at 2022-11-06

やりたいこと

[[1 2 3]]

という多次元スライスに

[4 5 6]

というスライスを足して

[[1 2 3] [4 5 6]]

にしたい

やり方

package main

import "fmt"

func main() {
    sliceA := [][]int{
        []int{1,2,3},
    }
    
    sliceB := []int{4, 5,6}
    
    sliceA = append(sliceA, sliceB)
    
    fmt.Println(sliceA) //[[1 2 3] [4 5 6]]
}

入れ物になる方の型[][]intはintのスライス([]int)のスライスという意味。
[]intが挿入する方の型と同じじゃないとエラーになるので注意

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?