title: How to get latitude and longitude from google map api using go language?
body: <p>How to get <strong>'lat'</strong> and <strong>'lng'</strong> from <strong>location</strong> in geometry using go language ?</p>
  
  <p>I'm trying to get latitude and longitude to use it for next api to fetch weather of specific location.</p>
  
  <p>I am getting an error when running the code: </p>
  
  <blockquote>
    <p>panic: runtime error: index out of range</p>
  </blockquote>
  
  <p>My response looks like this:
  <a href="https://developers.google.com/maps/documentation/geocoding/start" rel="nofollow noreferrer">https://developers.google.com/maps/documentation/geocoding/start</a></p>
  
  <p>My code is here.</p>
  
  <pre><code>package main
  
  import (
      "os"
      "fmt"
      "net/http"
      "log"
      "encoding/json"
      "io/ioutil"
  )
  
  const helloMessage = "Hello to the weather program. Please enter the name of the city and the weather will show."
  const googleApiUri = "https://maps.googleapis.com/maps/api/geocode/json?key=MYKEY&amp;address="
  
  
  
  type googleApiResponse struct {
      Results Results `json:"results"`
  }
  
  type Results []Geometry
  
  type Geometry struct {
      Geometry Location `json:"geometry"`
  }
  
  type Location struct {
      Location Coordinates `json:"location"`
  }
  
  type Coordinates struct {
      Latitude string `json:"lat"`
      Longitude string `json:"lng"`
  }
  
  
  func main() {
      fmt.Println(helloMessage)
  
      args := os.Args
      getCityCoordinates(args[0])
  }
  
  func getCityCoordinates(city string) {
      fmt.Println("Fetching langitude and longitude of the city ...")
      resp, err := http.Get(googleApiUri + city)
  
      if err != nil {
          log.Fatal("Fetching google api uri data error: ", err)
      }
  
      bytes, err := ioutil.ReadAll(resp.Body)
      defer resp.Body.Close()
      if err != nil {
          log.Fatal("Reading google api data error: ", err)
      }
  
      var data googleApiResponse
      json.Unmarshal(bytes, &amp;data)
      fmt.Println(data.Results[0].Geometry.Location.Latitude)
  
      fmt.Println("Fetching langitude and longitude ended successful ...")
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/a5ANM.png" rel="nofollow noreferrer">enter image description here</a></p>