From fbc792f67457b057b32552afdd436c7a581e1bcd Mon Sep 17 00:00:00 2001 From: tmeissner Date: Tue, 4 Aug 2020 00:45:46 +0200 Subject: [PATCH] Add activities 4.04 & 4.05 & exercices 4.13-4.17 --- chapter_04/activity_4.04/main.go | 13 ++++++++ chapter_04/activity_4.05/main.go | 11 +++++++ chapter_04/exercise_4.13/main.go | 21 +++++++++++++ chapter_04/exercise_4.14/main.go | 47 +++++++++++++++++++++++++++++ chapter_04/exercise_4.15/main.go | 32 ++++++++++++++++++++ chapter_04/exercise_4.16/main.go | 24 +++++++++++++++ chapter_04/exercise_4.17/main.go | 51 ++++++++++++++++++++++++++++++++ 7 files changed, 199 insertions(+) create mode 100644 chapter_04/activity_4.04/main.go create mode 100644 chapter_04/activity_4.05/main.go create mode 100644 chapter_04/exercise_4.13/main.go create mode 100644 chapter_04/exercise_4.14/main.go create mode 100644 chapter_04/exercise_4.15/main.go create mode 100644 chapter_04/exercise_4.16/main.go create mode 100644 chapter_04/exercise_4.17/main.go diff --git a/chapter_04/activity_4.04/main.go b/chapter_04/activity_4.04/main.go new file mode 100644 index 0000000..5cc2474 --- /dev/null +++ b/chapter_04/activity_4.04/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + + days := []string{ + "Monday", "Tuesday", "Wednesday", "Thursday", + "Friday", "Saturday", "Sunday"} + days = append(days[len(days)-1:], days[:len(days)-1]...) + fmt.Println(days) + +} diff --git a/chapter_04/activity_4.05/main.go b/chapter_04/activity_4.05/main.go new file mode 100644 index 0000000..3592bfb --- /dev/null +++ b/chapter_04/activity_4.05/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + + s := []string{"Good", "Good", "Bad", "Good", "Good"} + s = append(s[:2], s[3:]...) + fmt.Println(s) + +} diff --git a/chapter_04/exercise_4.13/main.go b/chapter_04/exercise_4.13/main.go new file mode 100644 index 0000000..6fcf2f6 --- /dev/null +++ b/chapter_04/exercise_4.13/main.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func getUsers() map[string]string { + + users := map[string]string{ + "305": "Sue", + "204": "Bob", + "631": "Jake", + } + users["073"] = "Tracy" + return users + +} + +func main() { + + fmt.Println("Users:", getUsers()) + +} diff --git a/chapter_04/exercise_4.14/main.go b/chapter_04/exercise_4.14/main.go new file mode 100644 index 0000000..9af352e --- /dev/null +++ b/chapter_04/exercise_4.14/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "os" +) + +func getUsers() map[string]string { + + return map[string]string{ + "305": "Sue", + "204": "Bob", + "631": "Jake", + "073": "Tracy", + } + +} + +func getUser(id string) (string, bool) { + + users := getUsers() + user, exists := users[id] + return user, exists + +} + +func main() { + + if len(os.Args) != 2 { + fmt.Println("User ID not passed") + os.Exit(1) + } + + userID := os.Args[1] + name, exists := getUser(userID) + + if !exists { + fmt.Printf("Passed user ID (%v) not found.\nUsers: \n", userID) + for key, value := range getUsers() { + fmt.Println(" ID:", key, "Name:", value) + } + os.Exit(1) + } + + fmt.Println("Name:", name) + +} diff --git a/chapter_04/exercise_4.15/main.go b/chapter_04/exercise_4.15/main.go new file mode 100644 index 0000000..c57e322 --- /dev/null +++ b/chapter_04/exercise_4.15/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "os" +) + +var users = map[string]string{ + "305": "Sue", + "204": "Bob", + "631": "Jake", + "073": "Tracy", +} + +func deleteUser(id string) { + + delete(users, id) + +} + +func main() { + + if len(os.Args) != 2 { + fmt.Println("User ID not passed") + os.Exit(1) + } + + userID := os.Args[1] + deleteUser(userID) + fmt.Println("Users:", users) + +} diff --git a/chapter_04/exercise_4.16/main.go b/chapter_04/exercise_4.16/main.go new file mode 100644 index 0000000..b88d091 --- /dev/null +++ b/chapter_04/exercise_4.16/main.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +type id string + +func getIDs() (id, id, id) { + + var id1 id + var id2 id = "1234-5678" + var id3 id + id3 = "1234-5678" + return id1, id2, id3 + +} + +func main() { + + id1, id2, id3 := getIDs() + fmt.Println("id1 == id2: ", id1 == id2) + fmt.Println("id2 == id3: ", id2 == id3) + fmt.Println("id2 == \"1234-5678\"", string(id2) == "1234-5678") + +} diff --git a/chapter_04/exercise_4.17/main.go b/chapter_04/exercise_4.17/main.go new file mode 100644 index 0000000..9683389 --- /dev/null +++ b/chapter_04/exercise_4.17/main.go @@ -0,0 +1,51 @@ +package main + +import "fmt" + +type user struct { + name string + age int + balance float64 + member bool +} + +func getUsers() []user { + + u1 := user{ + name: "Tracy", + age: 51, + balance: 98.43, + member: true, + } + + u2 := user{ + age: 19, + name: "Nick", + } + + u3 := user{ + "Bob", + 25, + 0, + false, + } + + var u4 user + u4.name = "Sue" + u4.age = 31 + u4.member = true + u4.balance = 17.09 + + return []user{u1, u2, u3, u4} + +} + +func main() { + + users := getUsers() + + for i := 0; i < len(users); i++ { + fmt.Printf("%v: %#v\n", i, users[i]) + } + +}