title: GO - get photos from flickr api
body: <p>I'm new to Go. I'm trying to build a simple app that lets users get photos from flickr by keying in a tag in a form. I'm stuck at my current code (pasted below) as nothing appears on the page /showimage when I click on the submit button. What am I doing wrong in the code?</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "html/template"
      "io/ioutil"
      "log"
      "net/http"
      "net/url"
      "os"
  )
  
  func main() {
      http.HandleFunc("/", handler)
      http.HandleFunc("/showimage", showimage)
      fmt.Println("listening...")
      err := http.ListenAndServe(GetPort(), nil)
      if err != nil {
          log.Fatal("ListenAndServe: ", err)
      }
  }
  
  func GetPort() string {
      var port = os.Getenv("PORT")
      if port == "" {
          port = "4747"
          fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
      }
      return ":" + port
  }
  
  func handler (w http.ResponseWriter, r *http.Request) {
      fmt.Fprint(w, rootForm)
  }
  
  const rootForm = 
      `&lt;!DOCTYPE html&gt;
      &lt;html&gt;
          &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
              &lt;title&gt;Flickr photos&lt;/title&gt;
          &lt;/head&gt;
          &lt;body&gt;
              &lt;h1&gt;Flickr photos&lt;/h1&gt;
              &lt;p&gt;Find photos by tags!&lt;/p&gt;
              &lt;form action="/showimage" method="post" accept-charset="utf-8"&gt;
                  &lt;input type="text" name="str" value="Type Tags..." id="str"&gt;
                  &lt;input type="submit" value=".. and see the images!"&gt;
              &lt;/form&gt;
          &lt;/body&gt;
      &lt;/html&gt;`
  
  var upperTemplate = template.Must(template.New("showimage").Parse(upperTemplateHTML))
  
  func showimage(w http.ResponseWriter, r *http.Request) {
      tag := r.FormValue("str")
      safeTag := url.QueryEscape(tag)
      apiKey := "MYAPIKEY"
      fullUrl := fmt.Sprintf("https://api.flickr.com/services/rest/?method=flickr.people.getPhotos&amp;api_key=%s&amp;tags=%s", apiKey, safeTag)
  
      client := &amp;http.Client{}
  
      req, err := http.NewRequest("GET", fullUrl, nil)
      if err != nil {
          log.Fatal("NewRequest: ", err)
          return
      }
  
      resp, requestErr := client.Do(req)
      if requestErr != nil {
          log.Fatal("Do: ", requestErr)
          return
      }
  
      defer resp.Body.Close()
  
  
  
      body, dataReadErr := ioutil.ReadAll(resp.Body)
      if dataReadErr != nil {
          log.Fatal("ReadAll: ", dataReadErr)
          return
      }
  
      res := make(map[string]map[string]map[string]interface{}, 0)
  
      json.Unmarshal(body, &amp;res)
  
      owner, _ := res["photos"]["photo"]["owner"]
      id, _ := res["photos"]["photo"]["id"]
  
      queryUrl := fmt.Sprintf("http://flickr.com/photos/%s/%s", owner, id)
  
  
  
      tempErr := upperTemplate.Execute(w, queryUrl)
      if tempErr != nil {
          http.Error(w, tempErr.Error(), http.StatusInternalServerError)
      }
  }
  
  const upperTemplateHTML =
  `&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;Display images&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h3&gt;Images&lt;/h3&gt;
          &lt;img src="{{html .}}" alt="Image" /&gt;
      &lt;/body&gt;
  &lt;/html&gt;`
  </code></pre>