LoginSignup
0
0

More than 3 years have passed since last update.

append関数で可変長引数を受け取る

Posted at

書籍:Go言語によるWebアプリケーション開発より。

組み込みの append 関数は可変長引数を受け取れるので、複数の要素を一度に追加できる。適切な型のスライスを持っているなら、その名前に続けて「...」と記述するとスライス中のそれぞれの項目を個別の引数として渡せる。

// 格納先のスライス初期化
gore> var options []string

// 格納要素の初期化
gore> arr := []string{"a","b","c"}
[]string{
  "a",
  "b",
  "c",
}

// 可変長引数でまとめて追加
gore> options = append(options, arr...)
[]string{
  "a",
  "b",
  "c",
}

// スライスの宣言と可変長引数による受け渡しを同時に実施
gore> options = append(options, []string{"d","e","f"}...)
[]string{
  "a",
  "b",
  "c",
  "d",
  "e",
  "f",
}
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