add: func exercises
This commit is contained in:
@ -0,0 +1,70 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func runCmd(input string, games []game, byID map[int]game) bool {
|
||||
fmt.Println()
|
||||
|
||||
cmd := strings.Fields(input)
|
||||
if len(cmd) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
switch cmd[0] {
|
||||
case "quit":
|
||||
return cmdQuit()
|
||||
|
||||
case "list":
|
||||
return cmdList(games)
|
||||
|
||||
case "id":
|
||||
return cmdByID(cmd, games, byID)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func cmdQuit() bool {
|
||||
fmt.Println("bye!")
|
||||
return false
|
||||
}
|
||||
|
||||
func cmdList(games []game) bool {
|
||||
for _, g := range games {
|
||||
printGame(g)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func cmdByID(cmd []string, games []game, byID map[int]game) bool {
|
||||
if len(cmd) != 2 {
|
||||
fmt.Println("wrong id")
|
||||
return true
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(cmd[1])
|
||||
if err != nil {
|
||||
fmt.Println("wrong id")
|
||||
return true
|
||||
}
|
||||
|
||||
g, ok := byID[id]
|
||||
if !ok {
|
||||
fmt.Println("sorry. i don't have the game")
|
||||
return true
|
||||
}
|
||||
|
||||
printGame(g)
|
||||
|
||||
return true
|
||||
}
|
52
25-functions/exercises/refactor-to-funcs-2/solution/games.go
Normal file
52
25-functions/exercises/refactor-to-funcs-2/solution/games.go
Normal file
@ -0,0 +1,52 @@
|
||||
// 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"
|
||||
|
||||
type item struct {
|
||||
id int
|
||||
name string
|
||||
price int
|
||||
}
|
||||
|
||||
type game struct {
|
||||
item
|
||||
genre string
|
||||
}
|
||||
|
||||
func load() (games []game) {
|
||||
games = addGame(games, newGame(1, 50, "god of war", "action adventure"))
|
||||
games = addGame(games, newGame(2, 40, "x-com 2", "strategy"))
|
||||
games = addGame(games, newGame(3, 20, "minecraft", "sandbox"))
|
||||
return
|
||||
}
|
||||
|
||||
func addGame(games []game, g game) []game {
|
||||
return append(games, g)
|
||||
}
|
||||
|
||||
func newGame(id, price int, name, genre string) game {
|
||||
return game{
|
||||
item: item{id: id, name: name, price: price},
|
||||
genre: genre,
|
||||
}
|
||||
}
|
||||
|
||||
func indexByID(games []game) (byID map[int]game) {
|
||||
byID = make(map[int]game)
|
||||
for _, g := range games {
|
||||
byID[g.id] = g
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func printGame(g game) {
|
||||
fmt.Printf("#%d: %-15q %-20s $%d\n",
|
||||
g.id, g.name, "("+g.genre+")", g.price)
|
||||
}
|
35
25-functions/exercises/refactor-to-funcs-2/solution/main.go
Normal file
35
25-functions/exercises/refactor-to-funcs-2/solution/main.go
Normal file
@ -0,0 +1,35 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
games := load()
|
||||
byID := indexByID(games)
|
||||
|
||||
fmt.Printf("Inanc's game store has %d games.\n", len(games))
|
||||
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
for {
|
||||
fmt.Printf(`
|
||||
> list : lists all the games
|
||||
> id N : queries a game by id
|
||||
> quit : quits
|
||||
|
||||
`)
|
||||
|
||||
if !in.Scan() || !runCmd(in.Text(), games, byID) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user