点击(此处)折叠或打开

  1. // Arrays
  2. package main

  3. import (
  4.     "fmt"
  5. )

  6. func main() {
  7.     //Here we create an array a that will hold exactly 5 ints. The type of elements and length are both part of the array’s type. By default an array is zero-valued, which for ints means 0s.
  8.     var a [5]int
  9.     fmt.Println("emp:", a)
  10.     //We can set a value at an index using the array[index] = value syntax, and get a value with array[index].
  11.     a[4] = 100
  12.     fmt.Println("set:", a)
  13.     fmt.Println("get:", a[4])
  14.     //The builtin len returns the length of an array.
  15.     fmt.Println("len:", len(a))
  16.     //Use this syntax to declare and initialize an array in one line.
  17.     b := [5]int{1, 2, 3, 4, 5}
  18.     fmt.Println("dcl:", b)
  19.     //Array types are one-dimensional, but you can compose types to build multi-dimensional data structures.
  20.     var twoD [2][3]int
  21.     for i := 0; i < 2; i++ {
  22.         for j := 0; j < 3; j++ {
  23.             twoD[i][j] = i + j
  24.         }
  25.     }
  26.     fmt.Println("2d: ", twoD)
  27. }
转自https://gobyexample.com

点击(此处)折叠或打开

  1. // Arrays
  2. package main

  3. import (
  4.     "fmt"
  5. )

  6. func main() {
  7.     //Here we create an array a that will hold exactly 5 ints. The type of elements and length are both part of the array’s type. By default an array is zero-valued, which for ints means 0s.
  8.     var a [5]int
  9.     fmt.Println("emp:", a)
  10.     //We can set a value at an index using the array[index] = value syntax, and get a value with array[index].
  11.     a[4] = 100
  12.     fmt.Println("set:", a)
  13.     fmt.Println("get:", a[4])
  14.     //The builtin len returns the length of an array.
  15.     fmt.Println("len:", len(a))
  16.     //Use this syntax to declare and initialize an array in one line.
  17.     b := [5]int{1, 2, 3, 4, 5}
  18.     fmt.Println("dcl:", b)
  19.     //Array types are one-dimensional, but you can compose types to build multi-dimensional data structures.
  20.     var twoD [2][3]int
  21.     for i := 0; i < 2; i++ {
  22.         for j := 0; j < 3; j++ {
  23.             twoD[i][j] = i + j
  24.         }
  25.     }
  26.     fmt.Println("2d: ", twoD)
  27. }


09-06 17:25