massive: move a lot of things
This commit is contained in:
84
x-tba/swapi-api-client/main.go
Normal file
84
x-tba/swapi-api-client/main.go
Normal file
@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// const
|
||||
// nil, string, int, float64, bool, comparison
|
||||
// variables
|
||||
// multiple short variables
|
||||
// assignment?
|
||||
// if
|
||||
// error handling
|
||||
// functions
|
||||
// returns
|
||||
// defer
|
||||
// struct
|
||||
// encoding/json
|
||||
// pointers
|
||||
// concurrency
|
||||
// select
|
||||
// chan receive
|
||||
// fmt
|
||||
// Printf
|
||||
// Sprintf
|
||||
// Errorf
|
||||
// net/http
|
||||
// Get
|
||||
// context/Context
|
||||
|
||||
// TODO: convert fmt calls to log
|
||||
// TODO: you can make the fetcher a library and main package the user
|
||||
// TODO: you can generate an html for the ship details? template pkg.
|
||||
|
||||
const timeout = 10 * time.Second
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
quit("give me a film id", len(args) != 1)
|
||||
|
||||
id, err := strconv.Atoi(args[0])
|
||||
quit("film id is incorrrect", err)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
// TODO: print the ship details to a text file (or any io.Writer)
|
||||
|
||||
film, err := fetchFilm(ctx, id)
|
||||
quit("Error occurred while fetching the film data", err)
|
||||
fmt.Println(film)
|
||||
|
||||
// a channel also can be used to print as they come
|
||||
ships := make([]Starship, len(film.Starships))
|
||||
err = fetchStarships(ctx, film.Starships, ships)
|
||||
quit("Error occurred while fetching starships", err)
|
||||
|
||||
fmt.Println("Ships used in the movie:")
|
||||
fmt.Println("------------------------")
|
||||
for _, ship := range ships {
|
||||
fmt.Println(ship)
|
||||
}
|
||||
}
|
||||
|
||||
func quit(message string, cond interface{}) {
|
||||
var quit bool
|
||||
|
||||
switch v := cond.(type) {
|
||||
case error:
|
||||
quit = true
|
||||
message += ": " + v.Error()
|
||||
case bool:
|
||||
quit = v
|
||||
}
|
||||
|
||||
if quit {
|
||||
fmt.Fprintln(os.Stderr, message)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user