arrays


package main
import "fmt"

func main() {
    var ages = []int{1, 2, 3, 4, 5}
    fmt.Println(ages)
}

// [1 2 3 4 5]

but doing this...

package main
import "fmt"

func main() {
    var ages = [15]int{1, 2, 3, 4, 5}
    fmt.Println(ages)
}

...you will get a different result:

// [1 2 3 4 5 0 0 0 0 0 0 0 0 0 0]

To access an element from the array...
package main
import "fmt"

func main() {
    var ages = []int{1, 2, 3, 4, 5}
    fmt.Println(ages)
    fmt.Println(len(ages))
    fmt.Println(ages[2])
}

/*
[1 2 3 4 5]
5
3
*/

1 comment:

  1. package main
    import "fmt"

    func main() {

    x := fmt.Println //ending tedium
    var ages = [12]int{1, 2, 3, 4, 5}
    var names = [5]string{"Peter", "Tom", "Jerry", "Nick"}
    x(ages)
    x(names)

    x(len(ages))
    x(ages[2])
    }

    ReplyDelete