title: How to check whether a URL is downloadable or not in golang?
body: <p>I'm trying to download a file fro the url to a local file.</p>
  <p>I wanted to test whether the requesting url is only file, if it is not a file it should return bad request</p>
  <p>Any help could be appreciated</p>
  <pre><code>package main
      
      import (
          &quot;fmt&quot;
          &quot;io&quot;
          &quot;net/http&quot;
          &quot;os&quot;
      )
      
      func main() {
          fileUrl := &quot;http://example.com/file.txt&quot;
          err := DownloadFile(&quot;./example.txt&quot;, fileUrl)
          if err != nil {
              panic(err)
          }
          fmt.Println(&quot;Downloaded: &quot; + fileUrl)
      }
      
      // DownloadFile will download a url to a local file.
      func DownloadFile(filepath string, url string) error {
      
          // Get the data
          resp, err := http.Get(url)
          if err != nil {
              return err
          }
          defer resp.Body.Close()
      
          // Create the file
          out, err := os.Create(filepath)
          if err != nil {
              return err
          }
          defer out.Close()
      
          // Write the body to file
          _, err = io.Copy(out, resp.Body)
          return err
      }
  </code></pre>