blah/files.go

45 lines
813 B
Go
Raw Normal View History

2023-10-12 13:54:19 +00:00
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
jsoniter "github.com/json-iterator/go"
)
func readJSONFile(filename string, dst any) error {
log.Printf("Reading %s\n", filename)
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
return jsoniter.ConfigFastest.NewDecoder(file).Decode(dst)
}
func downloadFile(url, filename string) error {
log.Printf("%s not found, downloading from %s \n", filename, url)
response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("request failed with status code: %d", response.StatusCode)
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, response.Body)
return err
}