gondb is a simple, transparent Go package for accessing the National Nutrient Database for Standard Reference API.
Successful API queries return native Go structs that can be used immediately, with no need for type assertions.
gondb implements the endpoints defined in the documentation: http://ndb.nal.usda.gov/ndb/doc/. More detailed information about the behavior of each particular endpoint can be found at the official documentation of the API.
$ go get -u github.com/abadojack/gondb
##Usage
import "github.com/abadojack/gondb"
A data.gov API key must be incorporated into each API request. Sign up now if you do not have a key.
api := gondb.NewClient(nil, "your-api-key")
Executing queries is simple.
result, _ := api.Search("cheese", nil)
for _, item := range result.Items {
fmt.Println(item.Ndbno)
}
The endpoints allow separate optional parameter; if desired, these can be passed as the final parameter.
v := url.Values{}
v.Set("ndbno", "01009")
v.Set("type", "f")
nutrientIDs := []string{"204", "205", "269"}
report, _ := api.GetNutrientReport(nutrientIDs, v)
Check the NDB documentation for the various parameters for each endpoint.
//A program to display the name and quantity of each nutrient in a raw mango.
package main
import (
"fmt"
"github.com/abadojack/gondb"
)
func main() {
api := gondb.NewClient(nil, "DEMO_KEY")
result, err := api.Search("fried chicken", nil)
if err != nil {
panic(err)
}
if len(result.Items) > 0 {
for _, item := range result.Items {
report, err := api.GetFoodReport(item.Ndbno, nil)
if err != nil {
panic(err)
}
for _, nutrient := range report.Food.Nutrients {
fmt.Println(nutrient.Name, nutrient.Value, nutrient.Unit)
}
}
}
}
gondb is free software licensed under the GNU LGPL license. Details provided in the LICENSE file.