DEV Community

Discussion on: The Golang Saga: A Coder’s Journey There and Back Again. Part 1: Leaving the Shire

Collapse
 
freimer profile image
Fred Reimer

Awesome. While not needed for the datasets due to the number of records and the default limit, you will likely want to add in pagination for the results from the NOAA site. That would be a good next challenge. You can try setting the limit to some small number, like 2 or 3, with the datasets endpoint.

Hint: it appears the default "offset" is 0, but they appear to start counting records at 1. Setting the offset to 0 and 1 gives the same data. If you assume records start at 0 you will get duplicates.

Another thing to try, although it may be a bit of a jump when just starting out, is to create a client for the NOAA API, or at least the beginnings of one. Create a structure to hold your API key and maybe a net.http client, and a function to return an initialized structure (maybe NewClient). You can then define methods, like maybe GetDatasets. Your main may look something like:

func main() {
    nc, err := NewClient("my NOAA token")
    if err != nil {
        fmt.Println("Error creating NOAA API client")
        return
    }
    datasets, err := nc.GetDatasets()
    if err != nil {
        fmt.Printf("Error retrieving datasets: %v\n", err)
        return
    }

    fmt.Printf("Datasets (%d):\n%+v\n", len(datasets), datasets)
}
Enter fullscreen mode Exit fullscreen mode

Once you have this, you could move the files over to a new directory, say noaa, and do something like:

import (
    "fmt"

        "github.com/olgazju/weather-project/noaa"
)

func main() {
    nc, err := noaa.NewClient("my NOAA token")
    if err != nil {
        fmt.Println("Error creating NOAA API client")
        return
    }
    datasets, err := nc.GetDatasets()
    if err != nil {
        fmt.Printf("Error retrieving datasets: %v\n", err)
        return
    }

    fmt.Printf("Datasets (%d):\n%+v\n", len(datasets), datasets)
}
Enter fullscreen mode Exit fullscreen mode

Looking forward to hearing more about your journey!