Go Sample Code: Current Weather by Zip Code

Here is an example that will retrieve current weather by zip code.

Go code:

            
package main

import (
    "encoding/json"
    "fmt"
    "net/http"
)

type Payload struct {
	City string
	State string
	TempF string
	TempC string
	Weather string
	WindMPH string
	WindDir string
	RelativeHumidity string
	VisibilityMiles string
	AirQualityIndex string
	AirQualityCode string
	AirQuality string
	Sunrise string
	Sunset string
	DaylightHours string
	DaylightMinutes string
	Code string
	Credits int
}

func main() {
    // Create the struct variable to store the decoded JSON response
    thePayload := Payload{}

    // Call the API
    // Can error check here too replacing underscore with 'err'
    response, _ := http.Get("https://api.interzoid.com/getweatherzipcode?license=
                YOURAPILICENSEKEY&zip=94111")

    if response.StatusCode != 200 {
        // Report any HTTP Errors
        fmt.Println("Error: ", response.Status)

    } else {
        // Decode the JSON and print a comma-delimited response,
        // one of many ways to format the response
        _ = json.NewDecoder(response.Body).Decode(&thePayload)

	// display some of the fields
        fmt.Println(thePayload.City + "," + thePayload.State +
		"," + thePayload.TempF +
		"," + thePayload.TempC +
                "," + thePayload.Weather)
    }
}
            

To execute from the command line:

            
> go run YOURFILENAME.go
            

Results:

            
San Francisco,CA,76.0,24.4,A Few Clouds and Breezy
            

If you do not have an API License Key, you can obtain one by registering here.

Return to Get Weather by Zip Code API page

Free API Credits
Register for an Interzoid API account and receive free trial credits
Company Name Matching API
Generate similarity keys to identify duplicate and inconsistent company name data
More Info...
Other Matching APIs
Generate similarity keys for other data types
More Info...
International Phone Demographics API
Test page load and other Web asset performance time with a simple API call
More Info...
Email Information API
Get email address validation and enhancement data
More Info...