Last active
August 29, 2015 14:14
-
-
Save giwa/0789ef4dfb4ff66f9375 to your computer and use it in GitHub Desktop.
Go by Example: Arrays ref: http://qiita.com/giwa@github/items/111ab1828ea5a72988a3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment