LoginSignup
35
19

More than 5 years have passed since last update.

Go言語: スライス(配列)の要素を取り出す方法

Last updated at Posted at 2014-08-05
http
package main

import (
    "fmt"
)

func main() {
    a := []int{1, 2, 3, 4, 5, 6}

    // 最初の要素
    fmt.Println(a[0]) // 1

    // 最後の要素
    fmt.Println(a[len(a)-1]) // 6

    // index >= 2 ~ index < 4 までの要素
    fmt.Println(a[2:4]) // [3 4]

    // すべての要素
    fmt.Println(a[:]) // [1 2 3 4 5 6]

    // 最初と最後の要素以外すべて
    fmt.Println(a[1:len(a) - 1]) // [2 3 4 5]
}

バージョン情報

  • golang: go1.3
35
19
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
35
19