Browse Source

Add activities 4.04 & 4.05 & exercices 4.13-4.17

master
T. Meissner 4 years ago
parent
commit
fbc792f674
7 changed files with 199 additions and 0 deletions
  1. +13
    -0
      chapter_04/activity_4.04/main.go
  2. +11
    -0
      chapter_04/activity_4.05/main.go
  3. +21
    -0
      chapter_04/exercise_4.13/main.go
  4. +47
    -0
      chapter_04/exercise_4.14/main.go
  5. +32
    -0
      chapter_04/exercise_4.15/main.go
  6. +24
    -0
      chapter_04/exercise_4.16/main.go
  7. +51
    -0
      chapter_04/exercise_4.17/main.go

+ 13
- 0
chapter_04/activity_4.04/main.go View File

@ -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)
}

+ 11
- 0
chapter_04/activity_4.05/main.go View File

@ -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)
}

+ 21
- 0
chapter_04/exercise_4.13/main.go View File

@ -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())
}

+ 47
- 0
chapter_04/exercise_4.14/main.go View File

@ -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)
}

+ 32
- 0
chapter_04/exercise_4.15/main.go View File

@ -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)
}

+ 24
- 0
chapter_04/exercise_4.16/main.go View File

@ -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")
}

+ 51
- 0
chapter_04/exercise_4.17/main.go View File

@ -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])
}
}

Loading…
Cancel
Save