add: arrays
This commit is contained in:
@ -14,19 +14,89 @@ func main() {
|
|||||||
myAge byte
|
myAge byte
|
||||||
herAge byte
|
herAge byte
|
||||||
|
|
||||||
// uncomment the code below and observe the error
|
// this declares an array with two byte elements
|
||||||
// wrongDeclaration [-1]byte
|
// its length : 2
|
||||||
|
// its element type: byte
|
||||||
zero [0]byte
|
|
||||||
ages [2]byte
|
ages [2]byte
|
||||||
|
|
||||||
|
// this declares an array with five string elements
|
||||||
|
// its length : 5
|
||||||
|
// its element type: string
|
||||||
|
tags [5]string
|
||||||
|
|
||||||
|
// this array doesn't occupy any memory space (its length is zero)
|
||||||
|
// its length : 0
|
||||||
|
// its element type: byte
|
||||||
|
zero [0]byte
|
||||||
|
|
||||||
|
// this array uses a constant expression
|
||||||
|
// its length : 3
|
||||||
|
// its element type: byte
|
||||||
agesExp [2 + 1]byte
|
agesExp [2 + 1]byte
|
||||||
|
|
||||||
tags [5]string
|
// uncomment the code below and observe the error
|
||||||
|
//
|
||||||
|
// wrongDeclaration [-1]byte
|
||||||
)
|
)
|
||||||
|
|
||||||
fmt.Printf("%d %d\n", myAge, herAge)
|
fmt.Printf("myAge : %d\n", myAge)
|
||||||
fmt.Printf("%#v\n", zero)
|
fmt.Printf("herAge : %d\n", herAge)
|
||||||
fmt.Printf("%#v\n", ages)
|
|
||||||
fmt.Printf("%#v\n", agesExp)
|
// Since arrays we've declared here don't have any elements,
|
||||||
fmt.Printf("%#v\n", tags)
|
// Go automatically sets all their elements to their zero values
|
||||||
|
// depending on their element type.
|
||||||
|
|
||||||
|
// #v verb prints the array's length, element type and its elements
|
||||||
|
fmt.Printf("ages : %#v\n", ages)
|
||||||
|
fmt.Printf("tags : %#v\n", tags)
|
||||||
|
fmt.Printf("zero : %#v\n", zero)
|
||||||
|
fmt.Printf("agesExp : %#v\n", agesExp)
|
||||||
|
|
||||||
|
// note:
|
||||||
|
// ages and agesExp get printed 0x0 because they're byte arrays.
|
||||||
|
// bytes are represented with hex values. 0x0 means 0.
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// GETTING AND SETTING ARRAY ELEMENTS
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
// Note:
|
||||||
|
//
|
||||||
|
// Since, I've already declared the ages variable above,
|
||||||
|
// and, to show you the example below, I needed to create a new block.
|
||||||
|
//
|
||||||
|
// ages variable below is in a new block below. So, it's a new variable.
|
||||||
|
//
|
||||||
|
// I did so because I need to change the element type of the ages array
|
||||||
|
// to int (or, subtracting from a byte results in wraparound).
|
||||||
|
{
|
||||||
|
var ages [2]int
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Printf("ages : %#v\n", ages)
|
||||||
|
fmt.Printf("ages's type : %T\n", ages)
|
||||||
|
|
||||||
|
fmt.Println("len(ages) :", len(ages))
|
||||||
|
fmt.Println("ages[0] :", ages[0])
|
||||||
|
fmt.Println("ages[1] :", ages[1])
|
||||||
|
fmt.Println("ages[len(ages)-1] :", ages[len(ages)-1])
|
||||||
|
|
||||||
|
// WRONG:
|
||||||
|
// fmt.Println("ages[-1] :", ages[-1])
|
||||||
|
// fmt.Println("ages[2] :", ages[2])
|
||||||
|
// fmt.Println("ages[len(ages)] :", ages[len(ages)])
|
||||||
|
|
||||||
|
ages[0] = 6
|
||||||
|
ages[1] -= 3
|
||||||
|
|
||||||
|
// WRONG:
|
||||||
|
// ages[0] = "Can I?"
|
||||||
|
|
||||||
|
fmt.Println("ages[0] :", ages[0])
|
||||||
|
fmt.Println("ages[1] :", ages[1])
|
||||||
|
|
||||||
|
ages[0] *= ages[1]
|
||||||
|
fmt.Println("ages[0] :", ages[0])
|
||||||
|
fmt.Println("ages[1] :", ages[1])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// STORY:
|
// STORY:
|
||||||
// Hipster's Love store publishes limited books
|
// Hipster's Love store publishes limited books
|
||||||
// twice a year.
|
// twice a year.
|
||||||
@ -31,10 +33,30 @@ func main() {
|
|||||||
books[2] = "Everythingship"
|
books[2] = "Everythingship"
|
||||||
books[3] += books[0] + " 2nd Edition"
|
books[3] += books[0] + " 2nd Edition"
|
||||||
|
|
||||||
|
// --------------------
|
||||||
|
// INDEXING
|
||||||
|
// --------------------
|
||||||
|
|
||||||
// Go compiler can catch indexing errors when constant is used
|
// Go compiler can catch indexing errors when constant is used
|
||||||
// books[4] = "Neverland"
|
// books[4] = "Neverland"
|
||||||
|
|
||||||
// Go compiler cannot catch indexing errors when non-constant is used
|
// Go compiler cannot catch indexing errors when non-constant is used
|
||||||
// i := 4
|
// i := 4
|
||||||
// books[i] = "Neverland"
|
// books[i] = "Neverland"
|
||||||
|
|
||||||
|
// --------------------
|
||||||
|
// PRINTING ARRAYS
|
||||||
|
// --------------------
|
||||||
|
|
||||||
|
// print the type
|
||||||
|
fmt.Printf("books : %T\n", books)
|
||||||
|
|
||||||
|
// print the elements
|
||||||
|
fmt.Println("books :", books)
|
||||||
|
|
||||||
|
// print the elements in quoted string
|
||||||
|
fmt.Printf("books : %q\n", books)
|
||||||
|
|
||||||
|
// print the elements along with their types
|
||||||
|
fmt.Printf("books : %#v\n", books)
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,20 @@ func main() {
|
|||||||
|
|
||||||
for i := range sBooks {
|
for i := range sBooks {
|
||||||
sBooks[i] = books[i+1]
|
sBooks[i] = books[i+1]
|
||||||
|
// changes to sBooks[i] will not be visible here.
|
||||||
|
// sBooks here is a copy of the original array.
|
||||||
}
|
}
|
||||||
|
// changes to sBooks are visible here
|
||||||
|
|
||||||
|
// sBooks is a copy of the original sBooks array.
|
||||||
|
//
|
||||||
|
// v is also a copy of the original array element.
|
||||||
|
//
|
||||||
|
// if you want to update the original element, use it as in the loop above.
|
||||||
|
//
|
||||||
|
// for _, v := range sBooks {
|
||||||
|
// v += "won't effect"
|
||||||
|
// }
|
||||||
|
|
||||||
fmt.Printf("\nwinter : %#v\n", wBooks)
|
fmt.Printf("\nwinter : %#v\n", wBooks)
|
||||||
fmt.Printf("\nsummer : %#v\n", sBooks)
|
fmt.Printf("\nsummer : %#v\n", sBooks)
|
||||||
|
@ -17,23 +17,64 @@ import "fmt"
|
|||||||
|
|
||||||
// So, let's create a 4 elements string array for the books.
|
// So, let's create a 4 elements string array for the books.
|
||||||
|
|
||||||
const (
|
|
||||||
winter = 1
|
|
||||||
summer = 3
|
|
||||||
yearly = winter + summer
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// ALTERNATIVE:
|
|
||||||
// Use this only when you don't know about the elements beforehand
|
// Use this only when you don't know about the elements beforehand
|
||||||
//
|
{
|
||||||
// var books [yearly]string
|
var books [4]string
|
||||||
|
|
||||||
books := [yearly]string{
|
books[0] = "Kafka's Revenge"
|
||||||
|
books[1] = "Stay Golden"
|
||||||
|
books[2] = "Everythingship"
|
||||||
|
books[3] += "Kafka's Revenge 2nd Edition"
|
||||||
|
|
||||||
|
_ = books
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is not necessary, use the short declaration syntax below
|
||||||
|
{
|
||||||
|
var books = [4]string{
|
||||||
"Kafka's Revenge",
|
"Kafka's Revenge",
|
||||||
"Stay Golden",
|
"Stay Golden",
|
||||||
"Everythingship",
|
"Everythingship",
|
||||||
"Kafka's Revenge 2nd Edition",
|
"Kafka's Revenge 2nd Edition",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = books
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use this if you know about the elements
|
||||||
|
{
|
||||||
|
books := [4]string{
|
||||||
|
"Kafka's Revenge",
|
||||||
|
"Stay Golden",
|
||||||
|
"Everythingship",
|
||||||
|
"Kafka's Revenge 2nd Edition",
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = books
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Use this if you know about the elements
|
||||||
|
books := [4]string{
|
||||||
|
"Kafka's Revenge",
|
||||||
|
"Stay Golden",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uninitialized elements will be set to their zero values
|
||||||
fmt.Printf("books : %#v\n", books)
|
fmt.Printf("books : %#v\n", books)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// You can also use the ellipsis syntax
|
||||||
|
// ... equals to 4
|
||||||
|
{
|
||||||
|
books := [...]string{
|
||||||
|
"Kafka's Revenge",
|
||||||
|
"Stay Golden",
|
||||||
|
"Everythingship",
|
||||||
|
"Kafka's Revenge 2nd Edition",
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = books
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -24,18 +24,11 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var books [yearly]string
|
books := [...]string{
|
||||||
|
"Kafka's Revenge",
|
||||||
books[0] = "Kafka's Revenge"
|
"Stay Golden",
|
||||||
books[1] = "Stay Golden"
|
"Everythingship",
|
||||||
books[2] = "Everythingship"
|
"Kafka's Revenge 2nd Edition",
|
||||||
books[3] += books[0] + " 2nd Edition"
|
|
||||||
|
|
||||||
// won't update the original array
|
|
||||||
for _, v := range books {
|
|
||||||
v += " (Sold Out)"
|
|
||||||
fmt.Println(v)
|
|
||||||
}
|
}
|
||||||
|
fmt.Printf("books : %#v\n", books)
|
||||||
fmt.Printf("\nbooks: %#v\n", books)
|
|
||||||
}
|
}
|
@ -1,35 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
// STORY:
|
|
||||||
// You want to compare two bookcases,
|
|
||||||
// whether they're equal or not.
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
type (
|
|
||||||
bookcase [5]int
|
|
||||||
cabinet [5]int
|
|
||||||
)
|
|
||||||
|
|
||||||
blue := bookcase{6, 9, 3, 2, 1}
|
|
||||||
red := cabinet{6, 9, 3, 2, 1}
|
|
||||||
|
|
||||||
fmt.Print("Are they equal? ")
|
|
||||||
|
|
||||||
if cabinet(blue) == red {
|
|
||||||
fmt.Println("✅")
|
|
||||||
} else {
|
|
||||||
fmt.Println("❌")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("blue: %#v\n", blue)
|
|
||||||
fmt.Printf("red : %#v\n", bookcase(red))
|
|
||||||
}
|
|
@ -21,9 +21,6 @@ func main() {
|
|||||||
books[i] += b + " 2nd Ed."
|
books[i] += b + " 2nd Ed."
|
||||||
}
|
}
|
||||||
|
|
||||||
// copying arrays using slices
|
|
||||||
// copy(books[:], prev[:])
|
|
||||||
|
|
||||||
books[3] = "Awesomeness"
|
books[3] = "Awesomeness"
|
||||||
|
|
||||||
fmt.Printf("last year:\n%#v\n", prev)
|
fmt.Printf("last year:\n%#v\n", prev)
|
@ -19,16 +19,16 @@ func main() {
|
|||||||
{9, 8, 4},
|
{9, 8, 4},
|
||||||
}
|
}
|
||||||
|
|
||||||
var avg float64
|
var sum float64
|
||||||
|
|
||||||
for _, grades := range students {
|
for _, grades := range students {
|
||||||
for _, grade := range grades {
|
for _, grade := range grades {
|
||||||
avg += grade
|
sum += grade
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const N = float64(len(students) * len(students[0]))
|
const N = float64(len(students) * len(students[0]))
|
||||||
fmt.Printf("Avg Grade: %g\n", avg/N)
|
fmt.Printf("Avg Grade: %g\n", sum/N)
|
||||||
|
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
// #2 - SO SO WAY
|
// #2 - SO SO WAY
|
||||||
@ -40,13 +40,13 @@ func main() {
|
|||||||
// [3]float64{9, 8, 4},
|
// [3]float64{9, 8, 4},
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// var avg float64
|
// var sum float64
|
||||||
|
|
||||||
// avg += students[0][0] + students[0][1] + students[0][2]
|
// sum += students[0][0] + students[0][1] + students[0][2]
|
||||||
// avg += students[1][0] + students[1][1] + students[1][2]
|
// sum += students[1][0] + students[1][1] + students[1][2]
|
||||||
|
|
||||||
// const N = float64(len(students) * len(students[0]))
|
// const N = float64(len(students) * len(students[0]))
|
||||||
// fmt.Printf("Avg Grade: %g\n", avg/N)
|
// fmt.Printf("Avg Grade: %g\n", sum/N)
|
||||||
|
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
// #3 - MANUAL WAY
|
// #3 - MANUAL WAY
|
||||||
@ -55,10 +55,10 @@ func main() {
|
|||||||
// student1 := [3]float64{5, 6, 1}
|
// student1 := [3]float64{5, 6, 1}
|
||||||
// student2 := [3]float64{9, 8, 4}
|
// student2 := [3]float64{9, 8, 4}
|
||||||
|
|
||||||
// var avg float64
|
// var sum float64
|
||||||
// avg += student1[0] + student1[1] + student1[2]
|
// sum += student1[0] + student1[1] + student1[2]
|
||||||
// avg += student2[0] + student2[1] + student2[2]
|
// sum += student2[0] + student2[1] + student2[2]
|
||||||
|
|
||||||
// const N = float64(len(student1) * 2)
|
// const N = float64(len(student1) * 2)
|
||||||
// fmt.Printf("Avg Grade: %g\n", avg/N)
|
// fmt.Printf("Avg Grade: %g\n", sum/N)
|
||||||
}
|
}
|
63
14-arrays/12-compare-unnamed/main.go
Normal file
63
14-arrays/12-compare-unnamed/main.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
// STORY:
|
||||||
|
// You want to compare two bookcases,
|
||||||
|
// whether they're equal or not.
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type (
|
||||||
|
// integer int
|
||||||
|
|
||||||
|
bookcase [5]int
|
||||||
|
cabinet [5]int
|
||||||
|
// ^- try changing this to: integer
|
||||||
|
// but first: uncomment the integer type above
|
||||||
|
)
|
||||||
|
|
||||||
|
blue := bookcase{6, 9, 3, 2, 1}
|
||||||
|
red := cabinet{6, 9, 3, 2, 1}
|
||||||
|
|
||||||
|
fmt.Print("Are they equal? ")
|
||||||
|
|
||||||
|
if cabinet(blue) == red {
|
||||||
|
fmt.Println("✅")
|
||||||
|
} else {
|
||||||
|
fmt.Println("❌")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("blue: %#v\n", blue)
|
||||||
|
fmt.Printf("red : %#v\n", bookcase(red))
|
||||||
|
|
||||||
|
// ------------------------------------------------
|
||||||
|
// The underlying type of an unnamed type is itself.
|
||||||
|
//
|
||||||
|
// [5]integer's underlying type: [5]integer
|
||||||
|
// [5]int's underlying type : [5]int
|
||||||
|
//
|
||||||
|
// > [5]integer and [5]int are different types.
|
||||||
|
// > Their memory layout is not important.
|
||||||
|
// > Their types are not the same.
|
||||||
|
|
||||||
|
// _ = [5]integer{} == [5]int{}
|
||||||
|
|
||||||
|
// ------------------------------------------------
|
||||||
|
// An unnamed and a named type can be compared,
|
||||||
|
// if they've identical underlying types.
|
||||||
|
//
|
||||||
|
// [5]integer's underlying type: [5]integer
|
||||||
|
// cabinet's underlying type : [5]integer
|
||||||
|
//
|
||||||
|
// Note: Assuming the cabinet's type definition is like so:
|
||||||
|
// type cabinet [5]integer
|
||||||
|
|
||||||
|
// _ = [5]integer{} == cabinet{}
|
||||||
|
}
|
2
14-arrays/TODO.md
Normal file
2
14-arrays/TODO.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
- [ ] add challenge link to the moodly's resources
|
||||||
|
- [ ] add exercises 1 and 2 after the array basics quiz
|
@ -1,54 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args[1:]
|
|
||||||
|
|
||||||
var (
|
|
||||||
sum float64
|
|
||||||
nums [5]float64
|
|
||||||
)
|
|
||||||
|
|
||||||
for i, v := range args {
|
|
||||||
n, err := strconv.ParseFloat(v, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nums[i] = n
|
|
||||||
sum += n
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Your numbers :", nums)
|
|
||||||
fmt.Println("Average :", sum/float64(len(nums)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// EXERCISE
|
|
||||||
// Average calculator has a flaw.
|
|
||||||
// It divides the numbers by the length of the array.
|
|
||||||
// This results in wrong calculatio.
|
|
||||||
//
|
|
||||||
// For example:
|
|
||||||
//
|
|
||||||
// When you run it like this:
|
|
||||||
// go run main.go 1 5
|
|
||||||
// It tells you that the average number is:
|
|
||||||
// 1.2
|
|
||||||
// Whereas, actually, it should be 3.
|
|
||||||
//
|
|
||||||
// Fix this error.
|
|
||||||
// So that, it will output 3 instead of 1.2
|
|
||||||
//
|
|
||||||
// Do not change the length of the array.
|
|
@ -1,36 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args[1:]
|
|
||||||
|
|
||||||
var (
|
|
||||||
sum float64
|
|
||||||
nums []float64
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, v := range args {
|
|
||||||
n, err := strconv.ParseFloat(v, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nums = append(nums, n)
|
|
||||||
sum += n
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Your numbers:", nums)
|
|
||||||
fmt.Println("Average:", sum/float64(len(nums)))
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args[1:]
|
|
||||||
|
|
||||||
var (
|
|
||||||
sum float64
|
|
||||||
nums [5]float64
|
|
||||||
total float64
|
|
||||||
)
|
|
||||||
|
|
||||||
for i, v := range args {
|
|
||||||
n, err := strconv.ParseFloat(v, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
total++
|
|
||||||
nums[i] = n
|
|
||||||
sum += n
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Your numbers:", nums)
|
|
||||||
fmt.Printf("(Only %g of them were valid)\n", total)
|
|
||||||
fmt.Println("Average:", sum/total)
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
const usage = `Sorry. Go arrays are fixed.
|
|
||||||
So, for now, I'm only supporting sorting %d numbers...
|
|
||||||
`
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args[1:]
|
|
||||||
|
|
||||||
var nums [5]float64
|
|
||||||
|
|
||||||
if len(args) > 5 {
|
|
||||||
fmt.Printf(usage, len(nums))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, v := range args {
|
|
||||||
n, err := strconv.ParseFloat(v, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nums[i] = n
|
|
||||||
}
|
|
||||||
|
|
||||||
for range nums {
|
|
||||||
for i, v := range nums {
|
|
||||||
if i < len(nums)-1 && v > nums[i+1] {
|
|
||||||
nums[i], nums[i+1] = nums[i+1], nums[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(nums)
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const corpus = `lazy cat jumps again and again and again...
|
|
||||||
since she is very excited and happy.`
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
query := os.Args[1:]
|
|
||||||
words := strings.Fields(corpus)
|
|
||||||
|
|
||||||
filter := [...]string{
|
|
||||||
"and", "or", "the", "is", "since", "very",
|
|
||||||
}
|
|
||||||
|
|
||||||
queries:
|
|
||||||
for _, q := range query {
|
|
||||||
for _, v := range filter {
|
|
||||||
if q == v {
|
|
||||||
continue queries
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, w := range words {
|
|
||||||
if q == w {
|
|
||||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
jake := "jake"
|
|
||||||
joe := "joe"
|
|
||||||
lee := "lee"
|
|
||||||
lina := "lina"
|
|
||||||
|
|
||||||
fmt.Println(jake)
|
|
||||||
fmt.Println(joe)
|
|
||||||
fmt.Println(lee)
|
|
||||||
fmt.Println(lina)
|
|
||||||
|
|
||||||
// for each name
|
|
||||||
// you need to declare a variable
|
|
||||||
// and then you need to print it
|
|
||||||
//
|
|
||||||
// but by using an array, you don't need to do that
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// instead of storing the names in variables,
|
|
||||||
// you can use an array to store all of the names
|
|
||||||
// in a single variable
|
|
||||||
|
|
||||||
// names := [3]string{"jake", "joe", "lee"}
|
|
||||||
names := [4]string{"jake", "joe", "lee", "lina"}
|
|
||||||
|
|
||||||
// doing so allows you to loop over the names
|
|
||||||
// and print each one of them
|
|
||||||
//
|
|
||||||
// you can't do this without arrays
|
|
||||||
for _, name := range names {
|
|
||||||
fmt.Println(name)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [3]int{15, 30, 25}
|
|
||||||
|
|
||||||
for _, age := range ages {
|
|
||||||
fmt.Println(age)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// uncomment the code below to observe the error
|
|
||||||
|
|
||||||
// ages := [3]int{15, 30, 25, 44}
|
|
||||||
|
|
||||||
// for _, age := range ages {
|
|
||||||
// fmt.Println(age)
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// you cannot use variables
|
|
||||||
// when setting the length of an array
|
|
||||||
|
|
||||||
// length := 3
|
|
||||||
|
|
||||||
// but, you can use constants and constant expressions
|
|
||||||
const length = 3 * 2
|
|
||||||
|
|
||||||
ages := [3 * 2]int{15, 30, 25}
|
|
||||||
|
|
||||||
for _, age := range ages {
|
|
||||||
fmt.Println(age)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// EXERCISE:
|
|
||||||
// Try to put the `length` constant
|
|
||||||
// in place of `3 * 2` above.
|
|
||||||
|
|
||||||
// EXERCISE:
|
|
||||||
// Try to put the `length` variable
|
|
||||||
// in place of `3 * 2` above.
|
|
||||||
//
|
|
||||||
// And observe the error.
|
|
||||||
//
|
|
||||||
// To do that, you need comment-out
|
|
||||||
// the length constant first.
|
|
@ -1,51 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// -----
|
|
||||||
// you cannot use incompatible values
|
|
||||||
// than the array's element type
|
|
||||||
//
|
|
||||||
// uncomment the code parts below one by one
|
|
||||||
// then observe the errors
|
|
||||||
// -----
|
|
||||||
|
|
||||||
// for example you can't use a string
|
|
||||||
// ages := [3]int{15, 30, "hi"}
|
|
||||||
|
|
||||||
// -----
|
|
||||||
|
|
||||||
// or a float64, etc...
|
|
||||||
// ages := [3]int{15, 30, 5.5}
|
|
||||||
|
|
||||||
// -----
|
|
||||||
|
|
||||||
// of course this is valid, since it's untyped
|
|
||||||
// 5. becomes 5 (int)
|
|
||||||
// ages := [3]int{15, 30, 5.}
|
|
||||||
|
|
||||||
// -----
|
|
||||||
|
|
||||||
// for _, age := range ages {
|
|
||||||
// fmt.Println(age)
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// EXERCISE:
|
|
||||||
// Try to put the `length` constant
|
|
||||||
// in place of `3 * 2` above.
|
|
||||||
|
|
||||||
// EXERCISE:
|
|
||||||
// Try to put the `length` variable
|
|
||||||
// in place of `3 * 2` above.
|
|
||||||
//
|
|
||||||
// And observe the error.
|
|
||||||
//
|
|
||||||
// To do that, you need comment-out
|
|
||||||
// the length constant first.
|
|
@ -1,20 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [...]int{15, 30, 25}
|
|
||||||
// equals to:
|
|
||||||
// ages := [3]int{15, 30, 25}
|
|
||||||
|
|
||||||
for _, age := range ages {
|
|
||||||
fmt.Println(age)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [3]int{15, 30, 25}
|
|
||||||
|
|
||||||
fmt.Printf("%T\n", ages) // [3]int
|
|
||||||
fmt.Printf("%T\n", [...]int{15, 30, 25}) // [3]int
|
|
||||||
|
|
||||||
fmt.Printf("%T\n", [2]int{15, 30}) // [2]int
|
|
||||||
|
|
||||||
fmt.Printf("%T\n", [1]string{"hi"}) // [1]string
|
|
||||||
fmt.Printf("%T\n", [...]float64{3.14, 6.28}) // [2]float64
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var ages [3]int
|
|
||||||
|
|
||||||
fmt.Println(ages)
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [3]int{1}
|
|
||||||
|
|
||||||
fmt.Println(ages)
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
names := [...]string{
|
|
||||||
"lina", "bob", "jane",
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("%#v\n", names)
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
names := [5]string{
|
|
||||||
"lina", "bob", "jane",
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("%#v\n", names)
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
names := [5]string{
|
|
||||||
"lina", "bob", "jane",
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("%#v\n", names)
|
|
||||||
|
|
||||||
temperatures := [10]float64{1.5, 2.5}
|
|
||||||
fmt.Printf("%#v\n", temperatures)
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [2]int{15, 20}
|
|
||||||
|
|
||||||
// i can directly use `len`
|
|
||||||
fmt.Println(len(ages)) // 2
|
|
||||||
|
|
||||||
// i can also assign its result to a variable
|
|
||||||
l := len(ages)
|
|
||||||
fmt.Println(l) // 2
|
|
||||||
|
|
||||||
// let's print the length of a few arrays
|
|
||||||
l = len([1]bool{true}) // 1
|
|
||||||
fmt.Println(l)
|
|
||||||
|
|
||||||
l = len([3]string{"lina", "james", "joe"}) // 3
|
|
||||||
fmt.Println(l)
|
|
||||||
|
|
||||||
// this array doesn't initialize any elements
|
|
||||||
// but its length is still 5
|
|
||||||
// whether the elements are initialized or not
|
|
||||||
l = len([5]int{})
|
|
||||||
fmt.Println(l) // 5
|
|
||||||
fmt.Println([5]int{})
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [2]int{15, 20}
|
|
||||||
|
|
||||||
// WRONG (try it):
|
|
||||||
// fmt.Println(ages[-1])
|
|
||||||
|
|
||||||
fmt.Println("1st item:", ages[0])
|
|
||||||
fmt.Println("2nd item:", ages[1])
|
|
||||||
|
|
||||||
// fmt.Println("2nd item:", ages[2])
|
|
||||||
|
|
||||||
// fmt.Println(ages[len(ages)])
|
|
||||||
// here, `len(ages) - 1` equals to 1
|
|
||||||
fmt.Println("last item:", ages[len(ages)-1])
|
|
||||||
|
|
||||||
// let's display the indexes and the items
|
|
||||||
// of the array
|
|
||||||
for i, v := range ages {
|
|
||||||
fmt.Printf("index: %d, value: %d\n", i, v)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ages := [2]int{15, 20}
|
|
||||||
|
|
||||||
ages[0] = 6
|
|
||||||
ages[1] *= 3
|
|
||||||
|
|
||||||
fmt.Println(ages)
|
|
||||||
|
|
||||||
// increase the last element by 1
|
|
||||||
ages[1]++
|
|
||||||
ages[len(ages)-1]++
|
|
||||||
|
|
||||||
fmt.Println(ages)
|
|
||||||
|
|
||||||
// v is a copy
|
|
||||||
for _, v := range ages {
|
|
||||||
// it's like:
|
|
||||||
// v = ages[0]
|
|
||||||
// v++
|
|
||||||
|
|
||||||
// and:
|
|
||||||
// v = ages[1]
|
|
||||||
// v++
|
|
||||||
|
|
||||||
v++
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(ages)
|
|
||||||
|
|
||||||
// you don't need to use blank-identifier for the value
|
|
||||||
// for i, _ := range ages {
|
|
||||||
|
|
||||||
for i := range ages {
|
|
||||||
ages[i]++
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(ages)
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [3]int{6, 9, 3}
|
|
||||||
b := [3]int{6, 9, 3}
|
|
||||||
|
|
||||||
if a == b {
|
|
||||||
fmt.Println("they're equal!")
|
|
||||||
|
|
||||||
// they're comparable
|
|
||||||
// since their types are identical
|
|
||||||
fmt.Printf("1st array's type is %T\n", a)
|
|
||||||
fmt.Printf("2nd array's type is %T\n", b)
|
|
||||||
|
|
||||||
// go compares arrays like this
|
|
||||||
// it compares the elements one by one
|
|
||||||
fmt.Println(a[0] == b[0])
|
|
||||||
fmt.Println(a[1] == b[1])
|
|
||||||
fmt.Println(a[2] == b[2])
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [3]int{6, 9, 3}
|
|
||||||
b := [3]int{3, 9, 6}
|
|
||||||
|
|
||||||
if a == b {
|
|
||||||
fmt.Println("they're equal!")
|
|
||||||
} else {
|
|
||||||
fmt.Println("they're not equal! a==b?", a == b)
|
|
||||||
|
|
||||||
// but, they're comparable
|
|
||||||
// since their types are identical
|
|
||||||
fmt.Printf("1st array's type is %T\n", a)
|
|
||||||
fmt.Printf("2nd array's type is %T\n", b)
|
|
||||||
|
|
||||||
// go compares arrays like this
|
|
||||||
// it compares the elements one by one
|
|
||||||
fmt.Println(a[0] == b[0]) // false
|
|
||||||
fmt.Println(a[1] == b[1]) // true
|
|
||||||
fmt.Println(a[2] == b[2]) // false
|
|
||||||
|
|
||||||
// actually Go will quit comparing these arrays
|
|
||||||
// once it sees unequal items
|
|
||||||
//
|
|
||||||
// so, it will stop the comparison (short-circuit)
|
|
||||||
// when it sees the first item is false
|
|
||||||
// and it will return false
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// these two arrays not comparable
|
|
||||||
// their types are different
|
|
||||||
|
|
||||||
// uncomment all the code below and observe the error
|
|
||||||
|
|
||||||
// a := [3]int{6, 9, 3}
|
|
||||||
// b := [2]int{6, 9}
|
|
||||||
|
|
||||||
// you can't ask this question
|
|
||||||
|
|
||||||
// if a == b {
|
|
||||||
// fmt.Println("they're equal!")
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [2]string{"jane", "lina"}
|
|
||||||
b := [2]string{"jane", "lina"}
|
|
||||||
c := [2]string{"mike", "joe"}
|
|
||||||
|
|
||||||
fmt.Println("a==b?", a == b) // equal
|
|
||||||
fmt.Println("a==c?", a == c) // not equal
|
|
||||||
|
|
||||||
// cannot compare
|
|
||||||
d := [3]string{"jane", "lina"}
|
|
||||||
// fmt.Println("c==d?", c == d)
|
|
||||||
|
|
||||||
fmt.Printf("type of c is %T\n", c)
|
|
||||||
fmt.Printf("type of d is %T\n", d)
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
type (
|
|
||||||
A [3]int
|
|
||||||
B [3]int
|
|
||||||
)
|
|
||||||
|
|
||||||
x := A{1, 2, 3}
|
|
||||||
y := B{1, 2, 3}
|
|
||||||
z := [3]int{1, 2, 3}
|
|
||||||
|
|
||||||
fmt.Printf("x's type: %T\n", x) // named type : main.A
|
|
||||||
fmt.Printf("y's type: %T\n", y) // named type : main.B
|
|
||||||
fmt.Printf("z's type: %T\n", z) // unnamed type: [3]int
|
|
||||||
|
|
||||||
// cannot compare different named types
|
|
||||||
// fmt.Println("x==y?", x == y)
|
|
||||||
|
|
||||||
// can convert between identical types
|
|
||||||
fmt.Println("x==y?", x == A(y))
|
|
||||||
fmt.Println("x==y?", B(x) == y)
|
|
||||||
|
|
||||||
fmt.Printf("x's type : %T\n", x)
|
|
||||||
fmt.Printf("A(y)'s type: %T\n", A(y))
|
|
||||||
|
|
||||||
// can compare to unnamed types
|
|
||||||
fmt.Println("x==z?", x == z)
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [3]int{5, 4, 7}
|
|
||||||
|
|
||||||
// this assignment will create a new array value
|
|
||||||
// and then it will be stored in the b variable
|
|
||||||
b := a
|
|
||||||
|
|
||||||
fmt.Println("a =>", a)
|
|
||||||
fmt.Println("b =>", b)
|
|
||||||
|
|
||||||
// they're equal, they're clones
|
|
||||||
fmt.Println("a==b?", a == b)
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [3]int{5, 4, 7}
|
|
||||||
b := a
|
|
||||||
|
|
||||||
// this will only change the 'a' array
|
|
||||||
a[0] = 10
|
|
||||||
|
|
||||||
fmt.Println("a =>", a)
|
|
||||||
fmt.Println("b =>", b)
|
|
||||||
|
|
||||||
// this will only change the 'b' array
|
|
||||||
b[1] = 20
|
|
||||||
|
|
||||||
fmt.Println("a =>", a)
|
|
||||||
fmt.Println("b =>", b)
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [3]int{5, 4, 7}
|
|
||||||
b := [3]int{6, 9, 3}
|
|
||||||
|
|
||||||
b = a
|
|
||||||
|
|
||||||
fmt.Println("b =>", b)
|
|
||||||
|
|
||||||
b[0] = 100
|
|
||||||
fmt.Println("a =>", a)
|
|
||||||
fmt.Println("b =>", b)
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// you can't assign the array 'a' to the array 'b'
|
|
||||||
// a's type is [2]int whereas b's type is [3]int
|
|
||||||
|
|
||||||
// when two values are not comparable,
|
|
||||||
// then they're not assignable either
|
|
||||||
|
|
||||||
// uncomment and observe the error
|
|
||||||
|
|
||||||
// a := [2]int{5, 4}
|
|
||||||
// b := [3]int{6, 9, 3}
|
|
||||||
|
|
||||||
// b = a
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a := [3]int{10, 4, 7}
|
|
||||||
|
|
||||||
// like the assignment passing an array to a function
|
|
||||||
// creates a copy of the array
|
|
||||||
fmt.Println("a (before) =>", a)
|
|
||||||
incr(a)
|
|
||||||
fmt.Println("a (after) =>", a)
|
|
||||||
}
|
|
||||||
|
|
||||||
// inside the function
|
|
||||||
// the passed array 'a' is a new copy (a clone)
|
|
||||||
// it's not the original one
|
|
||||||
func incr(a [3]int) {
|
|
||||||
// this only changes the copy of the passed array
|
|
||||||
a[1]++
|
|
||||||
|
|
||||||
// this prints the copied array not the original one
|
|
||||||
fmt.Println("a (inside) =>", a)
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// [LENGTH]TYPE {
|
|
||||||
// ELEMENTS
|
|
||||||
// }
|
|
||||||
|
|
||||||
// LENGTH=2 and TYPE=[2]int
|
|
||||||
|
|
||||||
// nums := [2][2]int{
|
|
||||||
// [2]int{2, 4},
|
|
||||||
// [2]int{1, 3},
|
|
||||||
// }
|
|
||||||
|
|
||||||
// code below is the same as the code above
|
|
||||||
nums := [2][2]int{
|
|
||||||
{2, 4},
|
|
||||||
{1, 3},
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("nums =", nums)
|
|
||||||
fmt.Println("nums[0] =", nums[0])
|
|
||||||
fmt.Println("nums[1] =", nums[1])
|
|
||||||
|
|
||||||
fmt.Println("nums[0][0] =", nums[0][0])
|
|
||||||
fmt.Println("nums[0][1] =", nums[0][1])
|
|
||||||
fmt.Println("nums[1][0] =", nums[1][0])
|
|
||||||
fmt.Println("nums[1][1] =", nums[1][1])
|
|
||||||
|
|
||||||
fmt.Println("len(nums) =", len(nums))
|
|
||||||
fmt.Println("len(nums[0]) =", len(nums[0]))
|
|
||||||
fmt.Println("len(nums[1]) =", len(nums[1]))
|
|
||||||
|
|
||||||
for i, array := range nums {
|
|
||||||
for j, n := range array {
|
|
||||||
// nums[i][j] = number
|
|
||||||
fmt.Printf("nums[%d][%d] = %d\n", i, j, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rates := [3]float64{
|
|
||||||
0.5,
|
|
||||||
2.5,
|
|
||||||
1.5,
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(rates)
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rates := [3]float64{
|
|
||||||
0: 0.5, // index: 0
|
|
||||||
1: 2.5, // index: 1
|
|
||||||
2: 1.5, // index: 2
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(rates)
|
|
||||||
|
|
||||||
// above array literal equals to this:
|
|
||||||
//
|
|
||||||
// rates := [3]float64{
|
|
||||||
// 0.5,
|
|
||||||
// 2.5,
|
|
||||||
// 1.5,
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rates := [3]float64{
|
|
||||||
1: 2.5, // index: 1
|
|
||||||
0: 0.5, // index: 0
|
|
||||||
2: 1.5, // index: 2
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(rates)
|
|
||||||
|
|
||||||
// above array literal equals to this:
|
|
||||||
//
|
|
||||||
// rates := [3]float64{
|
|
||||||
// 0.5,
|
|
||||||
// 2.5,
|
|
||||||
// 1.5,
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rates := [3]float64{
|
|
||||||
// index 0 empty
|
|
||||||
// index 1 empty
|
|
||||||
2: 1.5, // index: 2
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(rates)
|
|
||||||
|
|
||||||
// above array literal equals to this:
|
|
||||||
//
|
|
||||||
// rates := [3]float64{
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 1.5,
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// ellipsis (...) below calculates the length of the
|
|
||||||
// array automatically
|
|
||||||
rates := [...]float64{
|
|
||||||
// index 0 empty
|
|
||||||
// index 1 empty
|
|
||||||
// index 2 empty
|
|
||||||
// index 3 empty
|
|
||||||
// index 4 empty
|
|
||||||
5: 1.5, // index: 5
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(rates)
|
|
||||||
|
|
||||||
// above array literal equals to this:
|
|
||||||
//
|
|
||||||
// rates := [6]float64{
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 1.5,
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rates := [...]float64{
|
|
||||||
// index 1 to 4 empty
|
|
||||||
|
|
||||||
5: 1.5, // index: 5
|
|
||||||
2.5, // index: 6
|
|
||||||
0: 0.5, // index: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(rates)
|
|
||||||
|
|
||||||
// above array literal equals to this:
|
|
||||||
//
|
|
||||||
// rates := [7]float64{
|
|
||||||
// 0.5,
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 0.,
|
|
||||||
// 1.5,
|
|
||||||
// 2.5,
|
|
||||||
// }
|
|
||||||
}
|
|
@ -1,72 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
maxTurns = 5 // less is more difficult
|
|
||||||
usage = `Welcome to the Lucky Number Game!
|
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
|
||||||
Your mission is to guess one of those numbers.
|
|
||||||
|
|
||||||
The greater your number is, harder it gets.
|
|
||||||
|
|
||||||
Wanna play?
|
|
||||||
`
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
|
|
||||||
args := os.Args[1:]
|
|
||||||
|
|
||||||
if len(args) != 1 {
|
|
||||||
fmt.Printf(usage, maxTurns)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guess, err := strconv.Atoi(args[0])
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Not a number.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if guess < 0 {
|
|
||||||
fmt.Println("Please pick a positive number.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// This array will store the generated random numbers
|
|
||||||
var pickeds [maxTurns]int
|
|
||||||
|
|
||||||
for turn := 0; turn < maxTurns; turn++ {
|
|
||||||
n := rand.Intn(guess + 1)
|
|
||||||
|
|
||||||
pickeds[turn] = n
|
|
||||||
|
|
||||||
if n == guess {
|
|
||||||
fmt.Println("🎉 YOU WIN!")
|
|
||||||
goto pickeds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("☠️ YOU LOST... Try again?")
|
|
||||||
|
|
||||||
pickeds:
|
|
||||||
fmt.Println("Computer has picked these:", pickeds)
|
|
||||||
|
|
||||||
// after this line the program automatically exits
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// EXERCISE
|
|
||||||
// ?
|
|
||||||
//
|
|
||||||
// NOTE
|
|
||||||
// ?
|
|
||||||
//
|
|
||||||
// EXPECTED OUTPUT
|
|
||||||
// ?
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
@ -1,82 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
maxTurns = 5 // less is more difficult
|
|
||||||
usage = `Welcome to the Lucky Number Game!
|
|
||||||
|
|
||||||
The program will pick %d random numbers.
|
|
||||||
Your mission is to guess one of those numbers.
|
|
||||||
|
|
||||||
The greater your number is, harder it gets.
|
|
||||||
|
|
||||||
Wanna play?
|
|
||||||
`
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
|
|
||||||
args := os.Args[1:]
|
|
||||||
|
|
||||||
if len(args) != 1 {
|
|
||||||
fmt.Printf(usage, maxTurns)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guess, err := strconv.Atoi(args[0])
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Not a number.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if guess < 0 {
|
|
||||||
fmt.Println("Please pick a positive number.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// This array will store the generated random numbers
|
|
||||||
var pickeds [maxTurns]int
|
|
||||||
|
|
||||||
for turn := 0; turn < maxTurns; turn++ {
|
|
||||||
var n int
|
|
||||||
pick:
|
|
||||||
for {
|
|
||||||
n = rand.Intn(guess + 1)
|
|
||||||
for _, v := range pickeds {
|
|
||||||
if n == v {
|
|
||||||
continue pick
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
pickeds[turn] = n
|
|
||||||
|
|
||||||
if n == guess {
|
|
||||||
fmt.Println("🎉 YOU WIN!")
|
|
||||||
goto pickeds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("☠️ YOU LOST... Try again?")
|
|
||||||
|
|
||||||
pickeds:
|
|
||||||
fmt.Println("Computer has picked these:", pickeds)
|
|
||||||
|
|
||||||
// after this line the program automatically exits
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
## ?
|
|
||||||
* text *CORRECT*
|
|
||||||
* text
|
|
@ -1,23 +0,0 @@
|
|||||||
// 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/
|
|
||||||
//
|
|
||||||
|
|
||||||
const (
|
|
||||||
Mon Weekday = iota
|
|
||||||
Tue
|
|
||||||
Wed
|
|
||||||
Thu
|
|
||||||
Fri
|
|
||||||
Sat
|
|
||||||
Sun
|
|
||||||
)
|
|
||||||
|
|
||||||
var names = [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
|
||||||
|
|
||||||
|
|
||||||
for d := Mon; d <= Sun; d++ {
|
|
||||||
fmt.Println(names[d])
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
age1 := getAge(15)
|
|
||||||
age2 := getAge(25)
|
|
||||||
age3 := getAge(35)
|
|
||||||
|
|
||||||
fmt.Printf("age1 lives in %p\n", age1)
|
|
||||||
fmt.Printf("age2 lives in %p\n", age2)
|
|
||||||
fmt.Printf("age3 lives in %p\n", age3)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAge(n byte) *byte {
|
|
||||||
m := new(byte)
|
|
||||||
*m = n
|
|
||||||
return m
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
// 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 (
|
|
||||||
"encoding/csv"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
records := [...][3]string{
|
|
||||||
{"first_name", "last_name", "username"},
|
|
||||||
{"Rob", "Pike", "rob"},
|
|
||||||
{"Ken", "Thompson", "ken"},
|
|
||||||
{"Robert", "Griesemer", "gri"},
|
|
||||||
}
|
|
||||||
|
|
||||||
w := csv.NewWriter(os.Stdout)
|
|
||||||
|
|
||||||
for _, record := range records {
|
|
||||||
if err := w.Write(record[:]); err != nil {
|
|
||||||
log.Fatalln("error writing record to csv:", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write any buffered data to the underlying writer (standard output).
|
|
||||||
w.Flush()
|
|
||||||
|
|
||||||
if err := w.Error(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// Output:
|
|
||||||
// first_name,last_name,username
|
|
||||||
// Rob,Pike,rob
|
|
||||||
// Ken,Thompson,ken
|
|
||||||
// Robert,Griesemer,gri
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
# Array
|
|
||||||
|
|
||||||
## Summary
|
|
||||||
|
|
||||||
* An array can store any type of values as long as they've the same type
|
|
||||||
|
|
||||||
* Fixed Length and Type
|
|
||||||
* Stores the same type of values
|
|
||||||
|
|
||||||
* Array stores its values contagiously
|
|
@ -1,198 +0,0 @@
|
|||||||
// 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"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NOTE: We're going to refactor this code in the functions section.
|
|
||||||
|
|
||||||
const (
|
|
||||||
charDefault = "█"
|
|
||||||
charColon = "░"
|
|
||||||
charEmpty = " "
|
|
||||||
charSeparator = " "
|
|
||||||
|
|
||||||
// clears the screen
|
|
||||||
//
|
|
||||||
// only works on bash command prompts
|
|
||||||
// \033 is a control code: [2J clears the screen
|
|
||||||
//
|
|
||||||
// For Go Playground, use: "\f"
|
|
||||||
charClear = "\033[H\033[2J"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// for keeping things easy to read and type-safe
|
|
||||||
type placeholder [5][3]byte
|
|
||||||
|
|
||||||
// we're using arrays because:
|
|
||||||
// - the pattern of a placeholder is constant (it doesn't change)
|
|
||||||
// - the placeholders in the clock is constant (which is 8)
|
|
||||||
//
|
|
||||||
// so, whenever values are precomputed or constant, use an array.
|
|
||||||
//
|
|
||||||
// you can always convert it to a slice easily. you'll learn
|
|
||||||
// how to do so in the slices section.
|
|
||||||
digits := [10]placeholder{
|
|
||||||
// 0
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 1
|
|
||||||
{
|
|
||||||
{1, 1, 0},
|
|
||||||
{0, 1, 0},
|
|
||||||
{0, 1, 0},
|
|
||||||
{0, 1, 0},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 2
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 0},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 3
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 4
|
|
||||||
{
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 5
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 0},
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 6
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 0},
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 7
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 8
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
|
|
||||||
// 9
|
|
||||||
{
|
|
||||||
{1, 1, 1},
|
|
||||||
{1, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
{0, 0, 1},
|
|
||||||
{1, 1, 1},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
colon := placeholder{
|
|
||||||
{0, 0, 0},
|
|
||||||
{0, 1, 0},
|
|
||||||
{0, 0, 0},
|
|
||||||
{0, 1, 0},
|
|
||||||
{0, 0, 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
// For Go Playground: loop 100 steps, for example.
|
|
||||||
for {
|
|
||||||
// get the current time: hour, minute, and second
|
|
||||||
var (
|
|
||||||
now = time.Now()
|
|
||||||
hour, min, sec = now.Hour(), now.Minute(), now.Second()
|
|
||||||
)
|
|
||||||
|
|
||||||
// again, an array is used here.
|
|
||||||
//
|
|
||||||
// because, the number of placeholders in this clock
|
|
||||||
// is constant (which is 8 placeholders).
|
|
||||||
clock := [8]placeholder{
|
|
||||||
// separate the digits: 17 becomes, 1 and 7 respectively
|
|
||||||
digits[hour/10], digits[hour%10],
|
|
||||||
colon,
|
|
||||||
digits[min/10], digits[min%10],
|
|
||||||
colon,
|
|
||||||
digits[sec/10], digits[sec%10],
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(charClear)
|
|
||||||
|
|
||||||
// print the clock
|
|
||||||
for line := range clock[0] {
|
|
||||||
|
|
||||||
// print a line for each letter
|
|
||||||
for letter, v := range clock {
|
|
||||||
// colon blink
|
|
||||||
char := charDefault
|
|
||||||
if v == colon {
|
|
||||||
char = charEmpty
|
|
||||||
if sec%2 == 0 {
|
|
||||||
char = charColon
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// print a line
|
|
||||||
for _, c := range clock[letter][line] {
|
|
||||||
if c == 1 {
|
|
||||||
fmt.Print(char)
|
|
||||||
} else {
|
|
||||||
fmt.Print(charEmpty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(charSeparator)
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for 1 second
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +1,3 @@
|
|||||||
// 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
|
package main
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
@ -1,10 +1,3 @@
|
|||||||
// 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
|
package main
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -10,33 +10,46 @@ package main
|
|||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Declare empty arrays
|
// EXERCISE: Declare empty arrays
|
||||||
//
|
//
|
||||||
// 1. Declare and printf the following arrays:
|
// 1. Declare and print the following arrays with their types:
|
||||||
//
|
//
|
||||||
// 1. A string array with 4 items
|
// 1. The names of your best three friends
|
||||||
// 2. An int array 5 items
|
// 2. The distances to five different locations
|
||||||
// 3. A byte array with 5 items
|
// 3. A data buffer with five bytes of capacity
|
||||||
// 4. A float64 array with 1 item
|
// 4. Currency exchange ratios only for a single currency
|
||||||
// 5. A bool array with 4 items
|
// 5. Up/Down status of four different web servers
|
||||||
// 6. A byte array without any items
|
// 6. A byte array that doesn't occupy memory space
|
||||||
//
|
//
|
||||||
// 2. Print the types of the previous arrays.
|
// 2. Print only the types of the same arrays.
|
||||||
//
|
//
|
||||||
// NOTE
|
// 3. Print only the elements of the same arrays.
|
||||||
// You should use printf with #v verb.
|
//
|
||||||
|
// HINT
|
||||||
|
// When printing the elements of an array, you can use the usual Printf verbs.
|
||||||
|
//
|
||||||
|
// For example:
|
||||||
|
// When printing a string array, you can use "%q" verb as usual.
|
||||||
//
|
//
|
||||||
// EXPECTED OUTPUT
|
// EXPECTED OUTPUT
|
||||||
// names : [4]string{"", "", "", ""}
|
// names : [3]string{"", "", "", ""}
|
||||||
// distances: [5]int{0, 0, 0, 0, 0}
|
// distances: [5]int{0, 0, 0, 0, 0}
|
||||||
// data : [5]uint8{0x0, 0x0, 0x0, 0x0, 0x0}
|
// data : [5]uint8{0x0, 0x0, 0x0, 0x0, 0x0}
|
||||||
// ratios : [1]float64{0}
|
// ratios : [1]float64{0}
|
||||||
// switches : [4]bool{false, false, false, false}
|
// alives : [4]bool{false, false, false, false}
|
||||||
// zero : [0]bool{}
|
// zero : [0]uint8{}
|
||||||
// names : [4]string
|
//
|
||||||
|
// names : [3]string
|
||||||
// distances: [5]int
|
// distances: [5]int
|
||||||
// data : [5]uint8
|
// data : [5]uint8
|
||||||
// ratios : [1]float64
|
// ratios : [1]float64
|
||||||
// switches : [4]bool
|
// alives : [4]bool
|
||||||
// zero : [0]bool
|
// zero : [0]uint8
|
||||||
|
//
|
||||||
|
// names : ["" "" ""]
|
||||||
|
// distances: [0 0 0 0 0]
|
||||||
|
// data : [0 0 0 0 0]
|
||||||
|
// ratios : [0.00]
|
||||||
|
// alives : [false false false false]
|
||||||
|
// zero : []
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -10,34 +10,38 @@ package main
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 1. Declare and printf the following arrays:
|
|
||||||
// 1. A string array with 4 items
|
|
||||||
// 2. An int array 5 items
|
|
||||||
// 3. A byte array with 5 items
|
|
||||||
// 4. A float64 array with 1 item
|
|
||||||
// 5. A bool array with 4 items
|
|
||||||
// 6. A byte array without any items
|
|
||||||
var (
|
var (
|
||||||
names [4]string
|
names [3]string // The names of your best three friends
|
||||||
distances [5]int
|
distances [5]int // The distances to five different locations
|
||||||
data [5]byte
|
data [5]byte // A data buffer with five bytes of capacity
|
||||||
ratios [1]float64
|
ratios [1]float64 // Currency exchange ratios only for a single currency
|
||||||
switches [4]bool
|
alives [4]bool // Up/Down status of four different web servers
|
||||||
zero [0]bool
|
zero [0]byte // A byte array that doesn't occupy memory space
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 1. Declare and print the arrays with their types.
|
||||||
fmt.Printf("names : %#v\n", names)
|
fmt.Printf("names : %#v\n", names)
|
||||||
fmt.Printf("distances: %#v\n", distances)
|
fmt.Printf("distances: %#v\n", distances)
|
||||||
fmt.Printf("data : %#v\n", data)
|
fmt.Printf("data : %#v\n", data)
|
||||||
fmt.Printf("ratios : %#v\n", ratios)
|
fmt.Printf("ratios : %#v\n", ratios)
|
||||||
fmt.Printf("switches : %#v\n", switches)
|
fmt.Printf("alives : %#v\n", alives)
|
||||||
fmt.Printf("zero : %#v\n", zero)
|
fmt.Printf("zero : %#v\n", zero)
|
||||||
|
|
||||||
// 2. Print the types of the previous arrays.
|
// 2. Print only the types of the same arrays.
|
||||||
|
fmt.Println()
|
||||||
fmt.Printf("names : %T\n", names)
|
fmt.Printf("names : %T\n", names)
|
||||||
fmt.Printf("distances: %T\n", distances)
|
fmt.Printf("distances: %T\n", distances)
|
||||||
fmt.Printf("data : %T\n", data)
|
fmt.Printf("data : %T\n", data)
|
||||||
fmt.Printf("ratios : %T\n", ratios)
|
fmt.Printf("ratios : %T\n", ratios)
|
||||||
fmt.Printf("switches : %T\n", switches)
|
fmt.Printf("alives : %T\n", alives)
|
||||||
fmt.Printf("zero : %T\n", zero)
|
fmt.Printf("zero : %T\n", zero)
|
||||||
|
|
||||||
|
// 3. Print only the elements of the same arrays.
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Printf("names : %q\n", names)
|
||||||
|
fmt.Printf("distances: %d\n", distances)
|
||||||
|
fmt.Printf("data : %d\n", data)
|
||||||
|
fmt.Printf("ratios : %.2f\n", ratios)
|
||||||
|
fmt.Printf("alives : %t\n", alives)
|
||||||
|
fmt.Printf("zero : %d\n", zero)
|
||||||
}
|
}
|
||||||
|
118
14-arrays/exercises/02-get-set-arrays/main.go
Normal file
118
14-arrays/exercises/02-get-set-arrays/main.go
Normal 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() {
|
||||||
|
}
|
129
14-arrays/exercises/02-get-set-arrays/solution/main.go
Normal file
129
14-arrays/exercises/02-get-set-arrays/solution/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
20
14-arrays/exercises/03-array-literal/main.go
Normal file
20
14-arrays/exercises/03-array-literal/main.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Refactor to Array Literals
|
||||||
|
//
|
||||||
|
// 1. Use the 02-get-set-arrays exercise
|
||||||
|
//
|
||||||
|
// 2. Refactor the array assignments to array literals
|
||||||
|
//
|
||||||
|
// 1. You would need to change the array declarations to array literals
|
||||||
|
//
|
||||||
|
// 2. Then, you would need to move the right-hand side of the assignments,
|
||||||
|
// into the array literals.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// The output should be the same as the 02-get-set-arrays exercise.
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
121
14-arrays/exercises/03-array-literal/solution/main.go
Normal file
121
14-arrays/exercises/03-array-literal/solution/main.go
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
// 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() {
|
||||||
|
// The names of your best three friends
|
||||||
|
names := [3]string{
|
||||||
|
"Einstein",
|
||||||
|
"Tesla",
|
||||||
|
"Shepard",
|
||||||
|
}
|
||||||
|
|
||||||
|
// The distances to five different locations
|
||||||
|
distances := [5]int{50, 40, 75, 30, 125}
|
||||||
|
|
||||||
|
// A data buffer with five bytes of capacity
|
||||||
|
data := [5]byte{'H', 'E', 'L', 'L', 'O'}
|
||||||
|
|
||||||
|
// Currency exchange ratios only for a single currency
|
||||||
|
ratios := [1]float64{3.14145}
|
||||||
|
|
||||||
|
// Up/Down status of four different web servers
|
||||||
|
alives := [4]bool{true, false, true, false}
|
||||||
|
|
||||||
|
// A byte array that doesn't occupy memory space
|
||||||
|
//
|
||||||
|
// Don't do this:
|
||||||
|
// zero := [0]byte{}
|
||||||
|
//
|
||||||
|
// Do this (when you don't assign elements):
|
||||||
|
var zero [0]byte
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
18
14-arrays/exercises/04-ellipsis/main.go
Normal file
18
14-arrays/exercises/04-ellipsis/main.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Refactor to Ellipsis
|
||||||
|
//
|
||||||
|
// 1. Use the 03-array-literal exercise
|
||||||
|
//
|
||||||
|
// 2. Refactor the length of the array literals to ellipsis
|
||||||
|
//
|
||||||
|
// This means: Use the ellipsis instead of defining the array's length
|
||||||
|
// manually.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// The output should be the same as the 03-array-literal exercise.
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
117
14-arrays/exercises/04-ellipsis/solution/main.go
Normal file
117
14-arrays/exercises/04-ellipsis/solution/main.go
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
// 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() {
|
||||||
|
// The names of your best three friends
|
||||||
|
names := [...]string{
|
||||||
|
"Einstein",
|
||||||
|
"Tesla",
|
||||||
|
"Shepard",
|
||||||
|
}
|
||||||
|
|
||||||
|
// The distances to five different locations
|
||||||
|
distances := [...]int{50, 40, 75, 30, 125}
|
||||||
|
|
||||||
|
// A data buffer with five bytes of capacity
|
||||||
|
data := [...]byte{'H', 'E', 'L', 'L', 'O'}
|
||||||
|
|
||||||
|
// Currency exchange ratios only for a single currency
|
||||||
|
ratios := [...]float64{3.14145}
|
||||||
|
|
||||||
|
// Up/Down status of four different web servers
|
||||||
|
alives := [...]bool{true, false, true, false}
|
||||||
|
|
||||||
|
// A byte array that doesn't occupy memory space
|
||||||
|
// Obviously, do not use ellipsis on this one
|
||||||
|
var zero []byte
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
13
14-arrays/exercises/IDEAS.md
Normal file
13
14-arrays/exercises/IDEAS.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# Array Exercises
|
||||||
|
|
||||||
|
- get data from command-line
|
||||||
|
- into a fixed array; see how it blows beyond its len
|
||||||
|
|
||||||
|
- add items
|
||||||
|
- get items
|
||||||
|
- check the length
|
||||||
|
- reverse the array
|
||||||
|
- shuffle the items
|
||||||
|
- find the first item that contains x
|
||||||
|
- find the last item that contains y
|
||||||
|
- find the duplicate items
|
@ -1,20 +1,19 @@
|
|||||||
# Array Exercises
|
# Array Exercises
|
||||||
|
|
||||||
|
## Basic Exercises
|
||||||
|
|
||||||
1. **[Declare Empty Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/01-declare-empty)**
|
1. **[Declare Empty Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/01-declare-empty)**
|
||||||
|
|
||||||
- get data from command-line
|
2. **[Get and Set Array Elements](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/02-get-set-arrays)**
|
||||||
- into a fixed array; see how it blows beyond its len
|
|
||||||
|
|
||||||
- add items
|
3. **[Refactor to Array Literals](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/03-array-literal)**
|
||||||
- get items
|
|
||||||
- check the length
|
|
||||||
- print the items
|
|
||||||
- reverse the array
|
|
||||||
- shuffle the items
|
|
||||||
- find the first item that contains x
|
|
||||||
- find the last item that contains y
|
|
||||||
- find the duplicate items
|
|
||||||
|
|
||||||
1. **[text](https://github.com/inancgumus/learngo/tree/master/)**
|
4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)**
|
||||||
|
|
||||||
text
|
---
|
||||||
|
|
||||||
|
## Program Exercises
|
||||||
|
|
||||||
|
????. **[text](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/)**
|
||||||
|
|
||||||
|
?
|
||||||
|
@ -1,64 +1,3 @@
|
|||||||
# Arrays
|
# Array Quizzes
|
||||||
|
|
||||||
## Where is the 2nd variable below stored in memory?
|
|
||||||
```go
|
|
||||||
// Let's say that first variable below is stored in this memory location: 20th
|
|
||||||
var (
|
|
||||||
first int32 = 100
|
|
||||||
second int32 = 150
|
|
||||||
)
|
|
||||||
```
|
|
||||||
1. 21
|
|
||||||
2. 22
|
|
||||||
3. 24
|
|
||||||
4. It can be stored anywhere *CORRECT*
|
|
||||||
|
|
||||||
> **4:** That's right. It can be anywhere. Because, there's no guarantee that variables will be stored in contiguous memory locations.
|
|
||||||
|
|
||||||
|
|
||||||
## Where is the 3rd element of the following array stored in memory?
|
|
||||||
|
|
||||||
```go
|
|
||||||
//
|
|
||||||
// Let's say that:
|
|
||||||
// nums array is stored in this memory location (cell): 500th
|
|
||||||
//
|
|
||||||
// So, this means: nums[0] is stored at 500th location as well.
|
|
||||||
//
|
|
||||||
var nums [5]int64
|
|
||||||
```
|
|
||||||
1. 3
|
|
||||||
2. 2
|
|
||||||
3. 502
|
|
||||||
4. 503
|
|
||||||
5. 516 *CORRECT*
|
|
||||||
|
|
||||||
> **2:** Nope, that's the index of an element.
|
|
||||||
>
|
|
||||||
> **3, 4:** 500+index? You're getting closer.
|
|
||||||
>
|
|
||||||
> **5:** Perfect. Array elements are stored in contiguous memory locations (cells). Here, the array's location is 500, and each element is 8 bytes (int64). So, 1st element: 500th, 2nd element: 508th, 3rd element: 516th, and so on. Formula: 516 = 500 + (8 * (3 - 1)).
|
|
||||||
|
|
||||||
|
|
||||||
## How many values the following variable represents?
|
|
||||||
```go
|
|
||||||
var gophers [10]string
|
|
||||||
```
|
|
||||||
1. 0
|
|
||||||
2. 1 *CORRECT*
|
|
||||||
3. 2
|
|
||||||
4. 10
|
|
||||||
|
|
||||||
> **2:** That's right! A variable can only store one value. Here, it stores a single array value with all its elements. However, through the gophers variable, you can access to 10 string values individually of that array.
|
|
||||||
>
|
|
||||||
> **4:** That's the length of the array. It's not the number of values that the gophers variable represents.
|
|
||||||
|
|
||||||
|
|
||||||
## ?
|
|
||||||
1. text *CORRECT*
|
|
||||||
2. text
|
|
||||||
|
|
||||||
> **1:**
|
|
||||||
>
|
|
||||||
|
|
||||||
|
|
||||||
|
* [Array Basics](array-basics.md)
|
156
14-arrays/questions/array-basics.md
Normal file
156
14-arrays/questions/array-basics.md
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
# Arrays
|
||||||
|
|
||||||
|
## What's an array?
|
||||||
|
1. Accelerated charged particle array gun from Star Wars
|
||||||
|
2. Collection of values with dynamic length and type
|
||||||
|
3. Collection of values with fixed length and type *CORRECT*
|
||||||
|
|
||||||
|
|
||||||
|
## Where is the 2nd variable below stored in memory?
|
||||||
|
```go
|
||||||
|
// Let's say that first variable below is stored in this memory location: 20th
|
||||||
|
var (
|
||||||
|
first int32 = 100
|
||||||
|
second int32 = 150
|
||||||
|
)
|
||||||
|
```
|
||||||
|
1. 21
|
||||||
|
2. 22
|
||||||
|
3. 24
|
||||||
|
4. It can be stored anywhere *CORRECT*
|
||||||
|
|
||||||
|
> **4:** That's right. It can be anywhere. Because, unlike arrays, there isn't any guarantee that variables will be stored in contiguous memory locations.
|
||||||
|
|
||||||
|
|
||||||
|
## Where is the 3rd element of the nums array stored in memory?
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Let's say: nums array is stored in this memory location: 500th
|
||||||
|
var nums [5]int64
|
||||||
|
```
|
||||||
|
1. 3
|
||||||
|
2. 2
|
||||||
|
3. 502
|
||||||
|
4. 503
|
||||||
|
5. 516 *CORRECT*
|
||||||
|
|
||||||
|
> **2:** Nope, that's the index of an element.
|
||||||
|
>
|
||||||
|
> **3, 4:** 500+index? You're getting closer.
|
||||||
|
>
|
||||||
|
> **5:** Perfect. Array elements are stored in contiguous memory locations. Here, the array's location is 500, and each element is 8 bytes (int64). So, 1st element is stored in: 500th, 2nd element: 508th, 3rd element: 516th, and so on. Formula for math lovers: 516 = 500 + 8 * (3 - 1). General formula: Starting position + The size of each element * (Element's index position - 1).
|
||||||
|
|
||||||
|
|
||||||
|
## How many values this variable stores?
|
||||||
|
**HINT:** _I'm asking about the variable not about the array inside the variable._
|
||||||
|
```go
|
||||||
|
var gophers [10]string
|
||||||
|
```
|
||||||
|
1. 0
|
||||||
|
2. 1 *CORRECT*
|
||||||
|
3. 2
|
||||||
|
4. 10
|
||||||
|
|
||||||
|
> **2:** That's right! A variable can only store one value. Here, it stores a single array value. However, through the variable, you can also access to individual string values inside the array value.
|
||||||
|
>
|
||||||
|
> **4:** That's the length of the array. It's not the number of values that the gophers variable stores.
|
||||||
|
|
||||||
|
|
||||||
|
## What's the length of this array?
|
||||||
|
```go
|
||||||
|
var gophers [5]int
|
||||||
|
```
|
||||||
|
1. 5 *CORRECT*
|
||||||
|
2. 1
|
||||||
|
3. 2
|
||||||
|
|
||||||
|
> **1:** That's right! It stores 5 int values.
|
||||||
|
>
|
||||||
|
|
||||||
|
## What's the length of this array?
|
||||||
|
```go
|
||||||
|
const length = 5 * 2
|
||||||
|
var gophers [length - 1]int
|
||||||
|
```
|
||||||
|
1. 10
|
||||||
|
2. 9 *CORRECT*
|
||||||
|
3. 1
|
||||||
|
|
||||||
|
> **2:** That's right! 5 * 2 - 1 is 9. You can use constant expressions while declaring the length of an array.
|
||||||
|
|
||||||
|
|
||||||
|
## What's the element type of this array?
|
||||||
|
```go
|
||||||
|
var luminosity [100]float32
|
||||||
|
```
|
||||||
|
1. [100]float32
|
||||||
|
2. luminosity
|
||||||
|
3. float32 *CORRECT*
|
||||||
|
|
||||||
|
|
||||||
|
## What's the type of this array?
|
||||||
|
```go
|
||||||
|
var luminosity [100]float32
|
||||||
|
```
|
||||||
|
1. [100]float32 *CORRECT*
|
||||||
|
2. luminosity
|
||||||
|
3. float32
|
||||||
|
|
||||||
|
> **1:** That's right. Array's type is consisting of its length and its element type together.
|
||||||
|
>
|
||||||
|
|
||||||
|
|
||||||
|
## What does this program print?
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var names [3]string
|
||||||
|
|
||||||
|
names[len(names)-1] = "!"
|
||||||
|
names[1] = "think" + names[2]
|
||||||
|
names[0] = "Don't"
|
||||||
|
names[0] += " "
|
||||||
|
|
||||||
|
fmt.Println(names[0] + names[1] + names[2])
|
||||||
|
}
|
||||||
|
```
|
||||||
|
1. !think!Don't
|
||||||
|
2. Don't think!! *CORRECT*
|
||||||
|
3. This program is incorrect
|
||||||
|
|
||||||
|
> **2:** "Don't think!! Just do!". Explanation is here: https://play.golang.org/p/y_Tqwn_XRlg
|
||||||
|
>
|
||||||
|
|
||||||
|
|
||||||
|
## What does this program print?
|
||||||
|
_It's OK if you can't solve this question. This is a hard question. You may try it on Go Playground here: https://play.golang.org/p/o0o0UM7Ktyy_
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// This program sets the next element of the array,
|
||||||
|
// then it quits just before the last element.
|
||||||
|
func main() {
|
||||||
|
var sum [5]int
|
||||||
|
|
||||||
|
for i, v := range sum {
|
||||||
|
if i == len(sum) - 1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
sum[i+1] = 10
|
||||||
|
fmt.Print(v, " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
1. 0 0 0 0 *CORRECT*
|
||||||
|
2. 10 10 10 10
|
||||||
|
3. 0 10 10 10
|
||||||
|
|
||||||
|
> **1:** That's right! the for range clause copies the sum array. So, changing the elements of the sum array while inside of the loop won't effect the original sum array. However, if you try to print it right after the loop, you'll see that it has changed. Try printing it like so on Go Playground.
|
||||||
|
>
|
||||||
|
> **2:** That's not right. Because, the for range clause copies the sum array.
|
||||||
|
>
|
||||||
|
|
@ -1,52 +0,0 @@
|
|||||||
// 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"
|
|
||||||
|
|
||||||
// STORY:
|
|
||||||
// Hipster's Love store publishes limited books
|
|
||||||
// twice a year.
|
|
||||||
//
|
|
||||||
// The number of books they publish is fixed at 4.
|
|
||||||
|
|
||||||
// So, let's create a 4 elements string array for the books.
|
|
||||||
|
|
||||||
const (
|
|
||||||
winter = 1
|
|
||||||
summer = 3
|
|
||||||
yearly = winter + summer
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var (
|
|
||||||
prevBooks [yearly]string
|
|
||||||
books [yearly]string
|
|
||||||
)
|
|
||||||
|
|
||||||
books[0] = "Kafka's Revenge"
|
|
||||||
books[1] = "Stay Golden"
|
|
||||||
books[2] = "Everythingship"
|
|
||||||
books[3] += books[0] + " 2nd Edition"
|
|
||||||
|
|
||||||
prevBooks = books
|
|
||||||
books[0] = "Silver Ages"
|
|
||||||
books[1] = "Dragon's Fire"
|
|
||||||
books[2] = "Nothingless"
|
|
||||||
books[3] = prevBooks[2] + " 2nd Edition"
|
|
||||||
|
|
||||||
fmt.Println("Last Year's Books (Sold Out!):")
|
|
||||||
for _, v := range prevBooks {
|
|
||||||
fmt.Println("+", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("\nNew Books:")
|
|
||||||
for _, v := range books {
|
|
||||||
fmt.Println("+", v)
|
|
||||||
}
|
|
||||||
}
|
|
42
etc/stratch/main.go
Normal file
42
etc/stratch/main.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
students := [...][3]float64{
|
||||||
|
{5, 6, 1},
|
||||||
|
{9, 8, 4},
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum float64
|
||||||
|
for _, grades := range students {
|
||||||
|
for _, grade := range grades {
|
||||||
|
sum += grade
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const N = float64(len(students) * len(students[0]))
|
||||||
|
fmt.Printf("Avg Grade: %g\n", sum/N)
|
||||||
|
|
||||||
|
// students := [2][3]float64{
|
||||||
|
// [3]float64{5, 6, 1},
|
||||||
|
// [3]float64{9, 8, 4},
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var sum float64
|
||||||
|
// sum += students[0][0] + students[0][1] + students[0][2]
|
||||||
|
// sum += students[1][0] + students[1][1] + students[1][2]
|
||||||
|
|
||||||
|
// const N = float64(len(students) * len(students[0]))
|
||||||
|
// fmt.Printf("Avg Grade: %g\n", sum/N)
|
||||||
|
|
||||||
|
// student1 := [3]float64{5, 6, 1}
|
||||||
|
// student2 := [3]float64{9, 8, 4}
|
||||||
|
|
||||||
|
// var sum float64
|
||||||
|
// sum += student1[0] + student1[1] + student1[2]
|
||||||
|
// sum += student2[0] + student2[1] + student2[2]
|
||||||
|
|
||||||
|
// const N = float64(len(student1) * 2)
|
||||||
|
// fmt.Printf("Avg Grade: %g\n", sum/N)
|
||||||
|
}
|
160
x-tba/15-arrays-project-clock/01-challenge/main.go
Normal file
160
x-tba/15-arrays-project-clock/01-challenge/main.go
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||||
|
// ~GET SOCIAL~
|
||||||
|
//
|
||||||
|
// Tweet when you complete:
|
||||||
|
// http://bit.ly/GOTWEET-CLOCK
|
||||||
|
//
|
||||||
|
// Discuss it with other gophers:
|
||||||
|
// http://bit.ly/LEARNGOSLACK
|
||||||
|
// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// Notes
|
||||||
|
// + I've created the solutions step by step
|
||||||
|
// + So, if you get stuck, you can check out the next step
|
||||||
|
// without having to look at the entire final solution
|
||||||
|
// + For drawing the clock, you may use the artifacts below
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// ★ Usable Artifacts
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
|
||||||
|
// Clock Characters:
|
||||||
|
//
|
||||||
|
// You can put these in constants if you like.
|
||||||
|
//
|
||||||
|
// Use this for the digits : "█"
|
||||||
|
// Use this for the separators : "░"
|
||||||
|
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// ★ GOAL 1 : Printing the Digits
|
||||||
|
// ★ Solution: 02-printing-the-digits
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
|
||||||
|
// - [ ] Define a new placeholder type
|
||||||
|
// ...
|
||||||
|
|
||||||
|
// - [ ] Create the digits
|
||||||
|
// zero := ...
|
||||||
|
// one := ...
|
||||||
|
// ...
|
||||||
|
|
||||||
|
// - [ ] Put them into the "digits" array
|
||||||
|
// digits := ...
|
||||||
|
|
||||||
|
// - [ ] Print them side-by-side
|
||||||
|
// - [ ] Loop for all the lines in a digit
|
||||||
|
// - [ ] Print each digit line by line
|
||||||
|
//
|
||||||
|
// - [ ] Don't forget printing a space after each digit
|
||||||
|
// - [ ] Don't forget printing a newline after each line
|
||||||
|
//
|
||||||
|
// EXAMPLE: Let's say you want to print 10.
|
||||||
|
//
|
||||||
|
// ██ ███<--- Print a new line after printing a single line from
|
||||||
|
// █ █ █ all the digits.
|
||||||
|
// █ █ █
|
||||||
|
// █ █ █
|
||||||
|
// ███ ███
|
||||||
|
// ^^
|
||||||
|
// ||
|
||||||
|
// ++----> Add space between the digits
|
||||||
|
//
|
||||||
|
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// ★ GOAL 2 : Printing the Clock
|
||||||
|
// ★ Solution: 03-printing-the-clock
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
|
||||||
|
// - [ ] Get the current time
|
||||||
|
|
||||||
|
// - [ ] Get the current hour, minute and second from the current time
|
||||||
|
|
||||||
|
// - [ ] Create the clock array
|
||||||
|
//
|
||||||
|
// - [ ] Get the individual digits from the digits array
|
||||||
|
//
|
||||||
|
// clock := ...
|
||||||
|
|
||||||
|
// - [ ] Print the clock
|
||||||
|
//
|
||||||
|
// - [ ] In the loops, use the clocks array instead
|
||||||
|
|
||||||
|
// - [ ] Create a separator array (it's also a placeholder)
|
||||||
|
|
||||||
|
// - [ ] Add the separators into the correct positions of
|
||||||
|
// the clock array
|
||||||
|
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// ★ GOAL 3 : Updating the Clock
|
||||||
|
// ★ Solution: 04-updating-the-clock
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
|
||||||
|
// - [ ] Create an infinite loop to update the clock
|
||||||
|
|
||||||
|
// - [ ] Update the clock every second
|
||||||
|
//
|
||||||
|
// time.Sleep(time.Second) will stop the world for 1 second
|
||||||
|
//
|
||||||
|
// See this for more info:
|
||||||
|
// https://golang.org/pkg/time/#Sleep
|
||||||
|
|
||||||
|
// - [ ] Clear the screen before the infinite loop
|
||||||
|
//
|
||||||
|
// This string value clears the screen when printed:
|
||||||
|
//
|
||||||
|
// "\033[2J"
|
||||||
|
//
|
||||||
|
// + For Go Playground, use this instead: "\f"
|
||||||
|
//
|
||||||
|
// + This only works on bash command prompts.
|
||||||
|
|
||||||
|
// - [ ] Move the cursor to the top-left corner of the screen before each step
|
||||||
|
// of the infinite loop
|
||||||
|
//
|
||||||
|
// This string value moves the cursor like so when printed:
|
||||||
|
//
|
||||||
|
// "\033[H"
|
||||||
|
//
|
||||||
|
// + For Go Playground, use this instead: "\f"
|
||||||
|
//
|
||||||
|
// + This only works on bash command prompts.
|
||||||
|
|
||||||
|
// + If you're curious:
|
||||||
|
// \033 is a special control code:
|
||||||
|
// [2J clears the screen and the cursor
|
||||||
|
// [H moves the cursor to 0, 0 screen position
|
||||||
|
//
|
||||||
|
// See for more info:
|
||||||
|
// https://bluesock.org/~willkg/dev/ansi.html
|
||||||
|
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// ★ GOAL 4: Blinking the Separators
|
||||||
|
// ★ Solution: 05-blink-the-separators
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
|
||||||
|
// - [ ] Blink the separators
|
||||||
|
//
|
||||||
|
// They should be visible per two seconds.
|
||||||
|
//
|
||||||
|
// Example: 1st second invisible
|
||||||
|
// 2nd second visible
|
||||||
|
// 3rd second invisible
|
||||||
|
// 4th second visible
|
||||||
|
//
|
||||||
|
// HINT: There are two ways to do this.
|
||||||
|
//
|
||||||
|
// 1- Manipulating the clock array directly
|
||||||
|
// (by adding/removing the separators)
|
||||||
|
//
|
||||||
|
// 2- Deciding what to print when printing the clock
|
||||||
|
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
// YOU CAN FIND THE FINAL SOLUTION WITH ANNOTATIONS:
|
||||||
|
// 06-full-annotated-code
|
||||||
|
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
|
||||||
|
}
|
135
x-tba/15-arrays-project-clock/02-printing-the-digits/main.go
Normal file
135
x-tba/15-arrays-project-clock/02-printing-the-digits/main.go
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GOALS:
|
||||||
|
// 1. Define a new placeholder type
|
||||||
|
// 2. Create the digits
|
||||||
|
// 3. Put all of them in "digits" array
|
||||||
|
// 4. Print them side-by-side
|
||||||
|
|
||||||
|
// Use this for the digits : "█"
|
||||||
|
// Use this for the separators : "░"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// for keeping things easy to read and type-safe
|
||||||
|
type placeholder [5]string
|
||||||
|
|
||||||
|
zero := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
one := placeholder{
|
||||||
|
"██ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
two := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
three := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
four := placeholder{
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
five := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
six := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
seven := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
eight := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
nine := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
// This array's type is "like": [10][5]string
|
||||||
|
//
|
||||||
|
// However:
|
||||||
|
// + "placeholder" is not equal to [5]string in type-wise.
|
||||||
|
// + Because: "placeholder" is a defined type, which is different
|
||||||
|
// from [5]string type.
|
||||||
|
// + [5]string is an unnamed type.
|
||||||
|
// + placeholder is a named type.
|
||||||
|
// + The underlying type of [5]string and placeholder is the same:
|
||||||
|
// [5]string
|
||||||
|
digits := [...]placeholder{
|
||||||
|
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explanation: digits[0]
|
||||||
|
// + Each element of clock has the same length.
|
||||||
|
// + So: Getting the length of only one element is OK.
|
||||||
|
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
|
||||||
|
//
|
||||||
|
// The range clause below is ~equal to the following code:
|
||||||
|
// line := 0; line < 5; line++
|
||||||
|
for line := range digits[0] {
|
||||||
|
// Print a line for each placeholder in digits
|
||||||
|
for digit := range digits {
|
||||||
|
fmt.Print(digits[digit][line], " ")
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
146
x-tba/15-arrays-project-clock/03-printing-the-clock/main.go
Normal file
146
x-tba/15-arrays-project-clock/03-printing-the-clock/main.go
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
// 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"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GOALS:
|
||||||
|
// 1. Get time
|
||||||
|
// 2. Create the clock array
|
||||||
|
// Use the digits array
|
||||||
|
// 3. Print the digits
|
||||||
|
// Use the clock array instead of the digits array
|
||||||
|
// 4. Add the colons
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type placeholder [5]string
|
||||||
|
|
||||||
|
zero := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
one := placeholder{
|
||||||
|
"██ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
two := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
three := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
four := placeholder{
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
five := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
six := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
seven := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
eight := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
nine := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
colon := placeholder{
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
}
|
||||||
|
|
||||||
|
digits := [...]placeholder{
|
||||||
|
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||||
|
|
||||||
|
fmt.Printf("hour: %d, min: %d, sec: %d\n", hour, min, sec)
|
||||||
|
|
||||||
|
// [8][5]string
|
||||||
|
clock := [...]placeholder{
|
||||||
|
// extract the digits: 17 becomes, 1 and 7 respectively
|
||||||
|
digits[hour/10], digits[hour%10],
|
||||||
|
colon,
|
||||||
|
digits[min/10], digits[min%10],
|
||||||
|
colon,
|
||||||
|
digits[sec/10], digits[sec%10],
|
||||||
|
}
|
||||||
|
|
||||||
|
for line := range clock[0] {
|
||||||
|
for digit := range clock {
|
||||||
|
fmt.Print(clock[digit][line], " ")
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
// for line := range clock[0] {
|
||||||
|
// for digit := range clock {
|
||||||
|
// fmt.Print(clock[digit][line], " ")
|
||||||
|
// }
|
||||||
|
// fmt.Println()
|
||||||
|
// }
|
||||||
|
}
|
168
x-tba/15-arrays-project-clock/04-updating-the-clock/main.go
Normal file
168
x-tba/15-arrays-project-clock/04-updating-the-clock/main.go
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
// 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"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GOALS:
|
||||||
|
// 1. Update the clock in a loop
|
||||||
|
//
|
||||||
|
// 2. Before the whole loop:
|
||||||
|
// Clear the screen once
|
||||||
|
//
|
||||||
|
// 3. Before each step of the loop:
|
||||||
|
// Move the cursor to top-left position
|
||||||
|
|
||||||
|
// So, how to clear the screen and move the cursor?
|
||||||
|
// Check out the following constants.
|
||||||
|
//
|
||||||
|
// screenClear : When printed clears the screen.
|
||||||
|
// cursorMoveTop: Moves the cursor to top-left screen position
|
||||||
|
//
|
||||||
|
// + Note: This only works on bash command prompts.
|
||||||
|
//
|
||||||
|
// + For Go Playground, use these instead:
|
||||||
|
// screenClear = "\f"
|
||||||
|
// cursorMoveTop = "\f"
|
||||||
|
//
|
||||||
|
// See for more info: https://bluesock.org/~willkg/dev/ansi.html
|
||||||
|
const (
|
||||||
|
screenClear = "\033[2J"
|
||||||
|
cursorMoveTop = "\033[H"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type placeholder [5]string
|
||||||
|
|
||||||
|
zero := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
one := placeholder{
|
||||||
|
"██ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
two := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
three := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
four := placeholder{
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
five := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
six := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
seven := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
eight := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
nine := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
colon := placeholder{
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
}
|
||||||
|
|
||||||
|
digits := [...]placeholder{
|
||||||
|
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||||
|
}
|
||||||
|
|
||||||
|
// clears the command screen
|
||||||
|
fmt.Print(screenClear)
|
||||||
|
|
||||||
|
// Go Playground will not run an infinite loop.
|
||||||
|
// So, instead, you may loop for 1000 times:
|
||||||
|
// for i := 0; i < 1000; i++ {
|
||||||
|
for {
|
||||||
|
// moves the cursor to the top-left position
|
||||||
|
fmt.Print(cursorMoveTop)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||||
|
|
||||||
|
clock := [...]placeholder{
|
||||||
|
digits[hour/10], digits[hour%10],
|
||||||
|
colon,
|
||||||
|
digits[min/10], digits[min%10],
|
||||||
|
colon,
|
||||||
|
digits[sec/10], digits[sec%10],
|
||||||
|
}
|
||||||
|
|
||||||
|
for line := range clock[0] {
|
||||||
|
for digit := range clock {
|
||||||
|
fmt.Print(clock[digit][line], " ")
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
// pause for 1 second
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
150
x-tba/15-arrays-project-clock/05-blink-the-separators/main.go
Normal file
150
x-tba/15-arrays-project-clock/05-blink-the-separators/main.go
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
// 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"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GOAL:
|
||||||
|
// Blink the colons
|
||||||
|
|
||||||
|
const (
|
||||||
|
screenClear = "\033[2J"
|
||||||
|
cursorMoveTop = "\033[H"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type placeholder [5]string
|
||||||
|
|
||||||
|
zero := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
one := placeholder{
|
||||||
|
"██ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
two := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
three := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
four := placeholder{
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
five := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
six := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
seven := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
eight := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
nine := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
colon := placeholder{
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
}
|
||||||
|
|
||||||
|
digits := [...]placeholder{
|
||||||
|
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(screenClear)
|
||||||
|
|
||||||
|
for {
|
||||||
|
fmt.Print(cursorMoveTop)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||||
|
|
||||||
|
clock := [...]placeholder{
|
||||||
|
digits[hour/10], digits[hour%10],
|
||||||
|
colon,
|
||||||
|
digits[min/10], digits[min%10],
|
||||||
|
colon,
|
||||||
|
digits[sec/10], digits[sec%10],
|
||||||
|
}
|
||||||
|
|
||||||
|
for line := range clock[0] {
|
||||||
|
for index, digit := range clock {
|
||||||
|
// colon blink
|
||||||
|
next := clock[index][line]
|
||||||
|
if digit == colon && sec%2 == 0 {
|
||||||
|
next = " "
|
||||||
|
}
|
||||||
|
fmt.Print(next, " ")
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
// pause for 1 second
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
// fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
190
x-tba/15-arrays-project-clock/06-full-annotated-code/main.go
Normal file
190
x-tba/15-arrays-project-clock/06-full-annotated-code/main.go
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
// 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"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// screenClear : When printed clears the screen.
|
||||||
|
// cursorMoveTop: Moves the cursor to top-left screen position
|
||||||
|
//
|
||||||
|
// + Note: This only works on bash command prompts.
|
||||||
|
//
|
||||||
|
// + For Go Playground, use these instead:
|
||||||
|
// screenClear = "\f"
|
||||||
|
// cursorMoveTop = "\f"
|
||||||
|
//
|
||||||
|
// See for more info: https://bluesock.org/~willkg/dev/ansi.html
|
||||||
|
const (
|
||||||
|
screenClear = "\033[2J"
|
||||||
|
cursorMoveTop = "\033[H"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// for keeping things easy to read and type-safe
|
||||||
|
type placeholder [5]string
|
||||||
|
|
||||||
|
// put the digits (placeholders) into variables
|
||||||
|
// using the placeholder array type above
|
||||||
|
zero := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
one := placeholder{
|
||||||
|
"██ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
two := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
three := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
four := placeholder{
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
five := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
six := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
seven := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
eight := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
nine := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
colon := placeholder{
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
}
|
||||||
|
|
||||||
|
// This array's type is "like": [10][5]string
|
||||||
|
//
|
||||||
|
// However:
|
||||||
|
// + "placeholder" is not equal to [5]string in type-wise.
|
||||||
|
// + Because: "placeholder" is a defined type, which is different
|
||||||
|
// from [5]string type.
|
||||||
|
// + [5]string is an unnamed type.
|
||||||
|
// + placeholder is a named type.
|
||||||
|
// + The underlying type of [5]string and placeholder is the same:
|
||||||
|
// [5]string
|
||||||
|
digits := [...]placeholder{
|
||||||
|
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||||
|
}
|
||||||
|
|
||||||
|
// clears the command screen
|
||||||
|
fmt.Print(screenClear)
|
||||||
|
|
||||||
|
// Go Playground will not run an infinite loop.
|
||||||
|
// Loop for example 1000 times instead, like this:
|
||||||
|
// for i := 0; i < 1000; i++ { ... }
|
||||||
|
for {
|
||||||
|
// moves the cursor to the top-left position
|
||||||
|
fmt.Print(cursorMoveTop)
|
||||||
|
|
||||||
|
// get the current hour, minute and second
|
||||||
|
now := time.Now()
|
||||||
|
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||||
|
|
||||||
|
// extract the digits: 17 becomes, 1 and 7 respectively
|
||||||
|
clock := [...]placeholder{
|
||||||
|
digits[hour/10], digits[hour%10],
|
||||||
|
colon,
|
||||||
|
digits[min/10], digits[min%10],
|
||||||
|
colon,
|
||||||
|
digits[sec/10], digits[sec%10],
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explanation: clock[0]
|
||||||
|
// + Each element of clock has the same length.
|
||||||
|
// + So: Getting the length of only one element is OK.
|
||||||
|
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
|
||||||
|
//
|
||||||
|
// The range clause below is ~equal to the following code:
|
||||||
|
// line := 0; line < len(clock[0]); line++
|
||||||
|
for line := range clock[0] {
|
||||||
|
// Print a line for each placeholder in clock
|
||||||
|
for index, digit := range clock {
|
||||||
|
// Colon blink on every two seconds.
|
||||||
|
// + On each sec divisible by two, prints an empty line
|
||||||
|
// + Otherwise: prints the current pixel
|
||||||
|
next := clock[index][line]
|
||||||
|
if digit == colon && sec%2 == 0 {
|
||||||
|
next = " "
|
||||||
|
}
|
||||||
|
// Print the next line and,
|
||||||
|
// give it enough space for the next placeholder
|
||||||
|
fmt.Print(next, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// After each line of a placeholder, print a newline
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
// pause for 1 second
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
23
x-tba/15-arrays-project-clock/goals.md
Normal file
23
x-tba/15-arrays-project-clock/goals.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
## GOALS:
|
||||||
|
|
||||||
|
### STEP #1 — Create Digits
|
||||||
|
- [ ] Define a new placeholder type
|
||||||
|
- [ ] Create the digits
|
||||||
|
- [ ] Put them into the "digits" array
|
||||||
|
- [ ] Print them side-by-side
|
||||||
|
|
||||||
|
### STEP #2 — Print the Clock
|
||||||
|
- [ ] Get the current time
|
||||||
|
- [ ] Create the clock array
|
||||||
|
- [ ] Print the clock
|
||||||
|
- [ ] Add the separators
|
||||||
|
|
||||||
|
### STEP #3 — Animate the Clock
|
||||||
|
- [ ] Create an infinite loop to update the clock
|
||||||
|
- [ ] Update the clock every second
|
||||||
|
- [ ] Clear the screen before the infinite loop
|
||||||
|
- [ ] Move the cursor to the top-left corner of the screen before each
|
||||||
|
step of the infinite loop
|
||||||
|
|
||||||
|
### BONUS
|
||||||
|
- [ ] Blink the separators
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user