2
2

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 5 years have passed since last update.

Go by Example: Arrays

Last updated at Posted at 2015-01-24

(この記事は Go by Example: Arrays を翻訳したものです。)

Goに置いてarrayは番号がついた特定の長さの要素の連続である。

package main

import "fmt"

func main() {

	// 5つのintを持つarrayを作ります。
	// 要素の型と長さはarrayの型のである。
	// 最初からarrayはzero valueである。intsの場合は0
    var a [5]int
    fmt.Println("emp:", a)
    
	// indexに対応する値はarray[index] = valueの文法でセットできる。
	// 値の取得はarray[index]である。
    a[4] = 100
    fmt.Println("set:", a)
    fmt.Println("get:", a[4])
    
    // buildin関数のlenはarrayの長さを返す。
    fmt.Println("len:", len(a))
    
    // この文法をarrayをone lineで初期化するときに使ってください。
    b := [5]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)
    
    // array型は1次元だが、他の型を含めることにより多次元のデータ構造を表現できる。
    var twoD [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD)
}


$ go run arrays.go
emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]]


2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?