massive: move a lot of things

This commit is contained in:
Inanc Gumus
2019-04-26 21:32:20 +03:00
parent da30b109f8
commit 0547b1e320
105 changed files with 2624 additions and 192 deletions

View File

@ -0,0 +1,38 @@
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
)
// Film represents a single Star Wars film
type Film struct {
// All fields should be exported to be decoded
Title string `json:"title"`
Opening string `json:"opening_crawl"`
Starships []string `json:"starships"`
}
func (f Film) String() string {
var buf strings.Builder
fmt.Fprintln(&buf, f.Title)
fmt.Fprintln(&buf, strings.Repeat("-", len(f.Title)))
buf.WriteByte('\n')
fmt.Fprintln(&buf, f.Opening)
return buf.String()
}
// fetchFilm returns a Film resource by its id
func fetchFilm(ctx context.Context, id int) (film Film, err error) {
fmt.Println("requesting", fmt.Sprintf(swapi+"films/%d/", id))
c, err := request(ctx, fmt.Sprintf(swapi+"films/%d/", id))
if err != nil {
return film, err
}
return film, json.Unmarshal(c, &film)
}