-2

The simplest way to get file system in go is the code below.

http.Handle("/files", http.StripPrefix(pathPrefix, http.FileServer(root)))

But for the purpose of objective design, I prefer to wrap the body of function in the method like this.

f := file{}
http.Handle("/download", f.download)
http.Handle("/upload", f.upload)

How should I wrap the content of code in the file struct method?

Eklavya
  • 17,618
  • 4
  • 28
  • 57
ccd
  • 5,788
  • 10
  • 46
  • 96
  • 2
    Use `http.HandleFunc` as @BrunoReis suggested and [here](https://stackoverflow.com/questions/21957455/difference-between-http-handle-and-http-handlefunc) you find the difference between them and why you should use. – Eklavya May 30 '20 at 06:17

1 Answers1

1

You can do it by using http.HandleFunc rather than http.Handle:

f := file{}
http.HandleFunc("/download", f.download)
http.HandleFunc("/upload", f.upload)

Assuming it has the right signature (ie, assuming that f.download is defined as something like func (f file) download(w http.ResponseWriter, r *http.Request)), then it should do what you want.

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156