add: arrays

This commit is contained in:
Inanc Gumus
2018-11-17 21:56:09 +03:00
parent c0dbce8062
commit a3a0d39a0b
138 changed files with 2022 additions and 1905 deletions

View File

@@ -0,0 +1,118 @@
package main
// ---------------------------------------------------------
// EXERCISE: Get and Set Array Elements
//
// 1. Use the 01-declare-empty exercise
// 2. Remove everything but the array declarations
//
// 3. Assign your best friends' names to the names array
//
// 4. Assign distances to the closest cities to you to the distance array
//
// 5. Assign arbitrary bytes to the data array
//
// 6. Assign a value to the ratios array
//
// 7. Assign true/false values to the alives arrays
//
// 8. Try to assign to the zero array and observe the error
//
// 9. Now use ordinary loop statements for each array and print them
// (do not use for range)
//
// 10. Now use for range loop statements for each array and print them
//
// 11. Try assigning different types of values to the arrays, break things,
// and observe the errors
//
// 12. Remove some of the array assignments and observe the loop outputs
// (zero values)
//
//
// EXPECTED OUTPUT
//
// Note: The output can change depending on the values that you've assigned to them, of course.
// You're free to assign any values.
//
// names
// ====================
// names[0]: "Einstein"
// names[1]: "Tesla"
// names[2]: "Shepard"
//
// distances
// ====================
// distances[0]: 50
// distances[1]: 40
// distances[2]: 75
// distances[3]: 30
// distances[4]: 125
//
// data
// ====================
// data[0]: 72
// data[1]: 69
// data[2]: 76
// data[3]: 76
// data[4]: 79
//
// ratios
// ====================
// ratios[0]: 3.14
//
// alives
// ====================
// alives[0]: true
// alives[1]: false
// alives[2]: true
// alives[3]: false
//
// zero
// ====================
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FOR RANGES
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// names
// ====================
// names[0]: "Einstein"
// names[1]: "Tesla"
// names[2]: "Shepard"
//
// distances
// ====================
// distances[0]: 50
// distances[1]: 40
// distances[2]: 75
// distances[3]: 30
// distances[4]: 125
//
// data
// ====================
// data[0]: 72
// data[1]: 69
// data[2]: 76
// data[3]: 76
// data[4]: 79
//
// ratios
// ====================
// ratios[0]: 3.14
//
// alives
// ====================
// alives[0]: true
// alives[1]: false
// alives[2]: true
// alives[3]: false
//
// zero
// ====================
//
// ---------------------------------------------------------
func main() {
}

View File

@@ -0,0 +1,129 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"strings"
)
func main() {
var (
names [3]string // The names of your best three friends
distances [5]int // The distances to five different locations
data [5]byte // A data buffer with five bytes of capacity
ratios [1]float64 // Currency exchange ratios only for a single currency
alives [4]bool // Up/Down status of four different web servers
zero [0]byte // A byte array that doesn't occupy memory space
)
names[0] = "Einstein"
names[1] = "Tesla"
names[2] = "Shepard"
distances[0] = 50
distances[1] = 40
distances[2] = 75
distances[3] = 30
distances[4] = 125
data[0] = 'H'
data[1] = 'E'
data[2] = 'L'
data[3] = 'L'
data[4] = 'O'
ratios[0] = 3.14145
alives[0] = true
alives[1] = false
alives[2] = true
alives[3] = false
// zero[0] = "BOMB!"
_ = zero
// =========================================================================
separator := "\n" + strings.Repeat("=", 20) + "\n"
fmt.Print("names", separator)
for i := 0; i < len(names); i++ {
fmt.Printf("names[%d]: %q\n", i, names[i])
}
fmt.Print("\ndistances", separator)
for i := 0; i < len(distances); i++ {
fmt.Printf("distances[%d]: %d\n", i, distances[i])
}
fmt.Print("\ndata", separator)
for i := 0; i < len(data); i++ {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, data[i])
}
fmt.Print("\nratios", separator)
for i := 0; i < len(ratios); i++ {
fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i])
}
fmt.Print("\nalives", separator)
for i := 0; i < len(alives); i++ {
fmt.Printf("alives[%d]: %t\n", i, alives[i])
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i := 0; i < len(zero); i++ {
fmt.Printf("zero[%d]: %d\n", i, zero[i])
}
// =========================================================================
// you know how this works :) don't be freaked out!
fmt.Printf(`
%s
FOR RANGES
%[1]s
`, strings.Repeat("~", 30))
fmt.Print("names", separator)
for i, v := range names {
fmt.Printf("names[%d]: %q\n", i, v)
}
fmt.Print("\ndistances", separator)
for i, v := range distances {
fmt.Printf("distances[%d]: %d\n", i, v)
}
fmt.Print("\ndata", separator)
for i, v := range data {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, v)
}
fmt.Print("\nratios", separator)
for i, v := range ratios {
fmt.Printf("ratios[%d]: %.2f\n", i, v)
}
fmt.Print("\nalives", separator)
for i, v := range alives {
fmt.Printf("alives[%d]: %t\n", i, v)
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i, v := range zero {
fmt.Printf("zero[%d]: %d\n", i, v)
}
}