Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learn go basics #150

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0d85e1c
Added solution for 1 and 2
mzhou356 Mar 30, 2023
58bc9f4
Added solution for 03
mzhou356 Mar 30, 2023
f419e0d
Added solution for 04
mzhou356 Mar 30, 2023
aa55f2f
Added solution for 05
mzhou356 Mar 30, 2023
a4d117e
Added solution for 06
mzhou356 Mar 30, 2023
3563ebf
Added solution for 07
mzhou356 Mar 30, 2023
424f190
Added solution for 08
mzhou356 Mar 30, 2023
57a2105
Added solution for 09
mzhou356 Mar 30, 2023
db3e739
Added solution for 10
mzhou356 Mar 30, 2023
50e9f22
Added solutions for type-conversion.
mzhou356 Mar 30, 2023
332bdd8
Added solution for command line args.
mzhou356 Mar 30, 2023
08dc565
updated change in 06 of command line args.
mzhou356 Mar 30, 2023
00576d0
corrected conversion answer
mzhou356 Mar 30, 2023
b080927
Added exercises for strings.
mzhou356 Mar 31, 2023
04a501c
Added iota exercises.
mzhou356 Mar 31, 2023
53d20c5
Added exercises for printf.
mzhou356 Apr 1, 2023
9722bf1
Added solutions for parts of if statements.
mzhou356 Apr 2, 2023
3374618
Added solution for challenge
mzhou356 Apr 3, 2023
8eb871c
Added solution for 05-moving-rating.
mzhou356 Apr 4, 2023
eb23097
added solution for 01-richter-scale
mzhou356 Apr 11, 2023
579deed
Added solution for exercises for switch.
mzhou356 Apr 12, 2023
e2e1831
Added exercises for multiplication table
mzhou356 Apr 13, 2023
1d0520b
Added parts of solutions to exercises for for-loops.
mzhou356 Apr 14, 2023
bfded81
Added exercises for randomization game.
mzhou356 Apr 18, 2023
615c5d1
Added soluiton for labelled statement.
mzhou356 May 1, 2023
6be8b7c
completed moodly challenge.
mzhou356 May 2, 2023
9015a9c
Added solutions for exercise 1 to 7
mzhou356 May 7, 2023
58b0012
completed all exercises for arrays.
mzhou356 May 22, 2023
cc24975
Completed animating clock challenge.
mzhou356 May 22, 2023
966118d
Added simple follow up questions.
mzhou356 May 22, 2023
9977c9a
added exercise solution for go slices.
mzhou356 May 23, 2023
f8cdf56
added exercises for slices.
mzhou356 May 25, 2023
7f7a44c
Added slicing exericses for go.
mzhou356 May 26, 2023
543eefa
Added exercises for slice internals.
mzhou356 May 27, 2023
a4f0964
Added parts of the exercise solutions for advanced slice operation.
mzhou356 May 27, 2023
4887518
completed exercises for advanced slice ops exericses.
mzhou356 May 28, 2023
22e3661
rewrite solution from io utils to os.
mzhou356 May 28, 2023
c1639cf
updated the solution from io/utils to os.
mzhou356 May 28, 2023
0f3046a
changed from util/io -> os and added log.fatal.
mzhou356 May 28, 2023
d5e9090
added solution for sort a file.
mzhou356 May 28, 2023
dcda87e
Added solution for sort a file with ordinal.
mzhou356 May 28, 2023
aee6462
Added solution for print directories.
mzhou356 May 28, 2023
b624780
Added exercises for bouncing ball.
mzhou356 May 29, 2023
27e981c
Added exercises for go runes/string/bytes.
mzhou356 Jun 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added solutions for exercise 1 to 7
Signed-off-by: mindy <minzhou16@gmail.com>
mzhou356 committed May 7, 2023
commit 9015a9c66b22c9913a9d8d9a2ea36f800696ff4c
35 changes: 35 additions & 0 deletions 14-arrays/exercises/01-declare-empty/main.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@

package main

import "fmt"

// ---------------------------------------------------------
// EXERCISE: Declare empty arrays
//
@@ -59,4 +61,37 @@ package main
// ---------------------------------------------------------

func main() {
var (
names [3]string
distances [5]int
data [5]byte
ratios [1]float64
alives [4]bool
zero [0]byte
)
// 1. Declare and print the arrays with their types.
fmt.Printf("names : %#v\n", names)
fmt.Printf("distances: %#v\n", distances)
fmt.Printf("data : %#v\n", data)
fmt.Printf("ratios : %#v\n", ratios)
fmt.Printf("alives : %#v\n", alives)
fmt.Printf("zero : %#v\n", zero)

// 2. Print only the types of the same arrays.
fmt.Println()
fmt.Printf("names : %T\n", names)
fmt.Printf("distances: %T\n", distances)
fmt.Printf("data : %T\n", data)
fmt.Printf("ratios : %T\n", ratios)
fmt.Printf("alives : %T\n", alives)
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)
}
37 changes: 37 additions & 0 deletions 14-arrays/exercises/02-get-set-arrays/main.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@

package main

import "fmt"

// ---------------------------------------------------------
// EXERCISE: Get and Set Array Elements
//
@@ -123,4 +125,39 @@ package main
// ---------------------------------------------------------

func main() {
var (
names [3]string
distances [5]int
data [5]byte
ratios [1]float64
alives [4]bool
zero [0]byte
)
names[0] = "Jason"
names[1] = "Miles"
names[2] = "Eddie"

distances[0] = 70
distances[1] = 100
distances[2] = 120
distances[4] = 190

data[0] = 8
data[1] = 25
data[2] = 16
data[4] = 45

ratios[0] = 0.8

alives[1] = true
alives[3] = true

for i := 0; i < len(names); i++ {
fmt.Printf("names[%d]: %q\n", i, names[i])
}

for i := 0; i < len(zero); i++ {
fmt.Printf("zeros[%d]: %q", i, zero[i])
}

}
88 changes: 88 additions & 0 deletions 14-arrays/exercises/03-array-literal/main.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,11 @@

package main

import (
"fmt"
"strings"
)

// ---------------------------------------------------------
// EXERCISE: Refactor to Array Literals
//
@@ -25,4 +30,87 @@ package main
// ---------------------------------------------------------

func main() {
names := [3]string{"Einstein", "Tesla", "Shepard"}
distances := [5]int{50, 40, 75, 30, 125}
data := [5]byte{'H', 'E', 'L', 'L', 'O'}
ratios := [1]float64{3.14145}
alives := [4]bool{true, false, true, false}
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)
}
}
110 changes: 110 additions & 0 deletions 14-arrays/exercises/04-ellipsis/main.go
Original file line number Diff line number Diff line change
@@ -22,5 +22,115 @@ package main
// The output should be the same as the 03-array-literal exercise.
// ---------------------------------------------------------

import (
"fmt"
"strings"
)

func main() {
// The names of your three best 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
//
// Don't do this:
// zero := [0]byte{}
//
// Do this (when you don't assign elements):
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)
}
}
20 changes: 6 additions & 14 deletions 14-arrays/exercises/05-fix/main.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@

package main

import "fmt"

// ---------------------------------------------------------
// EXERCISE: Fix
//
@@ -19,19 +21,9 @@ package main
// ---------------------------------------------------------

func main() {
// var names [3]string = [3]string{
// "Einstein" "Shepard"
// "Tesla"
// }

// var books [5]string = [5]string{
// "Kafka's Revenge",
// "Stay Golden",
// "",
// "",
// ""
// }
names := [...]string{"Einstein", "Shepard", "Tesla"}
books := [5]string{"Kafka's Revenge", "Stay Golden"}

// fmt.Printf("%q\n", names)
// fmt.Printf("%q\n", books)
fmt.Printf("%#v\n", names)
fmt.Printf("%#v\n", books)
}
14 changes: 7 additions & 7 deletions 14-arrays/exercises/06-compare/main.go
Original file line number Diff line number Diff line change
@@ -24,20 +24,20 @@ import "fmt"
// ---------------------------------------------------------

func main() {
// week := [...]string{"Monday", "Tuesday"}
// wend := [4]string{"Saturday", "Sunday"}
week := [4]string{"Monday", "Tuesday"}
wend := [4]string{"Saturday", "Sunday"}

// fmt.Println(week != wend)
fmt.Println(week != wend)

// evens := [...]int{2, 4, 6, 8, 10}
// evens2 := [...]int32{2, 4, 6, 8, 10}
evens := [...]int32{2, 4, 6, 8, 10}
evens2 := [...]int32{2, 4, 6, 8, 10}

// fmt.Println(evens == evens2)
fmt.Println(evens == evens2)

// Use : uint8 for one of the arrays instead of byte here.
// Remember: Aliased types are the same types.
image := [5]byte{'h', 'i'}
var data [5]byte
var data [5]uint8

fmt.Println(data == image)
}
14 changes: 14 additions & 0 deletions 14-arrays/exercises/07-assign/main.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,11 @@

package main

import (
"fmt"
"strings"
)

// ---------------------------------------------------------
// EXERCISE: Assign the Arrays
//
@@ -42,4 +47,13 @@ package main
// ---------------------------------------------------------

func main() {
books := [...]string{"Kafka's Revenge", "Stay Golden", "Everythingship"}
upper, lower := books, books
for ind := range upper {
upper[ind] = strings.ToUpper(upper[ind])
lower[ind] = strings.ToLower(lower[ind])
}
fmt.Printf("books: %q\n", books)
fmt.Printf("upper: %q\n", upper)
fmt.Printf("lower: %q\n", lower)
}