I am new to Go ( and programming to an extent), so using a learn as you practice approach.
For the code below ( which pulls weather forecast ), I am looking to populate the longitude and latitude from a list I have.
I am not entirely sure how to setup that list in the first place - use structs and then pipe the value of the stored struct into this code? I tried that and it seems I can only hold one lon/lat pair in a struct. I was thinking of using maps but couldn't figure out how to set it all up. Perhaps, I should store the list of values in a text file and read from there:
p := Place{
placeName: "Accra", lat: "43.6595", long: "-79.3433",
placeName: "Kumasi", lat: "43.6595", long: "-79.3433",
placeName: "Tamale", lat: "43.6595", long: "-79.3433",
}
f, err := forecast.Get(key, p.lat, p.long, "now", forecast.CA)
if err != nil {
log.Fatal(err)
}
the struct looked like this ( I have about 10 separate long/lat values to use)
type Place struct {
placeName string
lat string
long string
}
The Place
struct is a single object, it has three properties, a name and lat and long. In order to have more than one you have to instantiate them individually, and commonly, you'd store them in a collection. In Go, the sensible choices would be an array, slice, or map.
Here is an example using a slice. The syntax for array is roughly the same. If you'd like I can provide an example that uses a map after I return from lunch. One other thing to note is that I'm allocating the collection using a syntax referred to as 'composite literal' there are other ways to do this... I could use the append
method or more conventional assignment to indexes/keys like myArray[0] = instanceOfPlaceThatIsAlreadyInitialized
.
places := []Place{
Place{placeName: "Accra", lat: "43.6595", long: "-79.3433"},
Place{placeName: "Kumasi", lat: "43.6595", long: "-79.3433"},
Place{placeName: "Tamale", lat: "43.6595", long: "-79.3433"},
}
Extended example; https://play.golang.org/p/EHBgn_I9dA
EDIT: Same thing with a map;
places := map[string]Place{
"Accra":Place{placeName: "Accra", lat: "43.6595", long: "-79.3433"},
"Kumasi":Place{placeName: "Kumasi", lat: "43.6595", long: "-79.3433"},
"Tamale": Place{placeName: "Tamale", lat: "43.6595", long: "-79.3433"},
}