From 799b7030fc6e2d5f6de4181948a308a1ae36c0ef Mon Sep 17 00:00:00 2001 From: Andinus Date: Mon, 16 Mar 2020 03:43:15 +0530 Subject: Export struct fields Only exported structs and fields are visible to other packages, including the encoding/json package. So you must capitalize the field names of the structs to export those fields so the Marshal func can see them --- cmd/cetus-bing/cetus-bing.go | 44 +++++++++++++++++++-------------------- cmd/cetus-nasa/cetus-nasa.go | 49 ++++++++++++++++++++++---------------------- pkg/cetus/cetus.go | 2 +- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/cmd/cetus-bing/cetus-bing.go b/cmd/cetus-bing/cetus-bing.go index b8c397c..29ef6f4 100644 --- a/cmd/cetus-bing/cetus-bing.go +++ b/cmd/cetus-bing/cetus-bing.go @@ -13,19 +13,19 @@ import ( ) type photo struct { - startDate string `json:"startdate"` - fullStartDate string `json:"fullstartdate"` - endDate string `json:"enddate"` - url string `json:"url"` - urlBase string `json:"urlbase"` - copyright string `json:"copyright"` - copyrightLink string `json:"copyrightlink"` - title string `json:"title"` - hsh string `json:"hsh"` + StartDate string `json:"startdate"` + FullStartDate string `json:"fullstartdate"` + EndDate string `json:"enddate"` + Url string `json:"url"` + UrlBase string `json:"urlbase"` + Copyright string `json:"copyright"` + CopyrightLink string `json:"copyrightlink"` + Title string `json:"title"` + Hsh string `json:"hsh"` } type bpod struct { - photos []photo `json:"images"` + Photos []photo `json:"images"` } var ( @@ -63,16 +63,16 @@ func main() { // if random was set then bpodRes holds list of multiple // responses, choose a random response from the list - var i int = rand.Intn(len(bpod.photos)) - bpodPhoto := bpod.photos[i] + var i int = rand.Intn(len(bpod.Photos)) + bpodPhoto := bpod.Photos[i] // correct image path - bpodPhoto.url = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.url) + bpodPhoto.Url = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.Url) // correct date format - dt, err := time.Parse("20060102", bpodPhoto.startDate) + dt, err := time.Parse("20060102", bpodPhoto.StartDate) cetus.ErrChk("bpodPhoto.startDate parse failed", err) - bpodPhoto.startDate = dt.Format("2006-01-02") + bpodPhoto.StartDate = dt.Format("2006-01-02") printDetails(bpodPhoto) @@ -81,7 +81,7 @@ func main() { return } - err = background.Set(bpodPhoto.url) + err = background.Set(bpodPhoto.Url) cetus.ErrChk("setting background failed", err) } @@ -105,14 +105,14 @@ func printDetails(bpodPhoto photo) { return } if pathOnly { - cetus.PrintPath(bpodPhoto.url) + cetus.PrintPath(bpodPhoto.Url) return } - fmt.Printf("Title: %s\n\n", bpodPhoto.title) - fmt.Printf("Copyright: %s\n", bpodPhoto.copyright) - fmt.Printf("Copyright Link: %s\n", bpodPhoto.copyrightLink) - fmt.Printf("Date: %s\n\n", bpodPhoto.startDate) - fmt.Printf("URL: %s\n", bpodPhoto.url) + fmt.Printf("Title: %s\n\n", bpodPhoto.Title) + fmt.Printf("Copyright: %s\n", bpodPhoto.Copyright) + fmt.Printf("Copyright Link: %s\n", bpodPhoto.CopyrightLink) + fmt.Printf("Date: %s\n\n", bpodPhoto.StartDate) + fmt.Printf("URL: %s\n", bpodPhoto.Url) } func bpodBody() (string, error) { diff --git a/cmd/cetus-nasa/cetus-nasa.go b/cmd/cetus-nasa/cetus-nasa.go index fafd921..d71c41d 100644 --- a/cmd/cetus-nasa/cetus-nasa.go +++ b/cmd/cetus-nasa/cetus-nasa.go @@ -13,17 +13,17 @@ import ( ) type apod struct { - copyright string `json:"copyright"` - date string `json:"date"` - explanation string `json:"explanation"` - hdURL string `json:"hdurl"` - mediaType string `json:"media_type"` - serviceVersion string `json:"service_version"` - title string `json:"title"` - url string `json:"url"` - - code int `json:"code"` - msg string `json:"msg"` + Copyright string `json:"copyright"` + Date string `json:"date"` + Explanation string `json:"explanation"` + HdURL string `json:"hdurl"` + MediaType string `json:"media_type"` + ServiceVersion string `json:"service_version"` + Title string `json:"title"` + Url string `json:"url"` + + Code int `json:"code"` + Msg string `json:"msg"` } var ( @@ -63,8 +63,8 @@ func main() { apod := apod{} err = json.Unmarshal([]byte(body), &apod) cetus.ErrChk("body unmarshal failed", err) - if len(apod.msg) != 0 { - log.Println("Message: ", apod.msg) + if len(apod.Msg) != 0 { + log.Println("Message: ", apod.Msg) } printDetails(apod) @@ -75,11 +75,10 @@ func main() { } // if media type is an image then set background - if apod.mediaType == "image" { - err = background.Set(apod.hdURL) + if apod.MediaType == "image" { + err = background.Set(apod.HdURL) cetus.ErrChk("setting background failed", err) } - } func parseFlags() { @@ -108,19 +107,19 @@ func printDetails(apod apod) { return } if pathOnly { - cetus.PrintPath(apod.hdURL) + cetus.PrintPath(apod.HdURL) return } - fmt.Printf("Title: %s\n\n", apod.title) - fmt.Printf("Copyright: %s\n", apod.copyright) - fmt.Printf("Date: %s\n\n", apod.date) - fmt.Printf("Media Type: %s\n", apod.mediaType) - if apod.mediaType == "image" { - fmt.Printf("URL: %s\n\n", apod.hdURL) + fmt.Printf("Title: %s\n\n", apod.Title) + fmt.Printf("Copyright: %s\n", apod.Copyright) + fmt.Printf("Date: %s\n\n", apod.Date) + fmt.Printf("Media Type: %s\n", apod.MediaType) + if apod.MediaType == "image" { + fmt.Printf("URL: %s\n\n", apod.HdURL) } else { - fmt.Printf("URL: %s\n\n", apod.url) + fmt.Printf("URL: %s\n\n", apod.Url) } - fmt.Printf("Explanation: %s\n", apod.explanation) + fmt.Printf("Explanation: %s\n", apod.Explanation) } func apodBody() (string, error) { diff --git a/pkg/cetus/cetus.go b/pkg/cetus/cetus.go index dbf72f8..90e9f4a 100644 --- a/pkg/cetus/cetus.go +++ b/pkg/cetus/cetus.go @@ -5,7 +5,7 @@ import ( "log" ) -var version string = "v0.4.8" +var version string = "v0.4.9" // Version prints cetus version func Version() { -- cgit 1.4.1-2-gfad0