HTTP服务器和客户端下载多个文件

I have tried setting up a download server and a download client for individual files. How can I modify them to serve/download all the files from a directory?

Following are my server and client codes:

//server.go
func main() {
        http.HandleFunc("/dlpath", handle)
        err := http.ListenAndServe(":10001", nil)
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }

func handle(writer http.ResponseWriter, r *http.Request) {
    filename := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve\\send.txt"
    http.ServeFile(writer, r, filename)
}

//client.go
func main() {
    downloadFile("res_out.txt", "http://localhost:10001/dlpath")
}

func downloadFile(dirname string, url string) error {

    // Create the file
    out, err := os.OpenFile(dirname, os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0666)
    if err != nil {
        fmt.Println(err)
    }
    defer out.Close()

    // get data
    request, err := http.NewRequest("GET", url, nil)
    if err != nil {
        fmt.Println(err)
    }

    client := http.Client{}
    resp, err := client.Do(request)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    // Write the body to file
    _, err = io.Copy(out, resp.Body)
    if err != nil {
        return err
    }
    return nil
}

I tried serving the directory in the handle function like so:

dirname := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve"
http.FileServer(http.Dir(dirname))

and tried to print out the response on the client side, but I got nothing. How can I serve all the files from the /serve directory, and download them in the client?

EDIT: Following are the contents of the serve directory:

serve ---sample.txt ---send.txt ---dir2 ------abc.txt

How can I download all these files on the client side as separate files, with the directory structure intact?

Update: When I call the http.Handle function (as mentioned in the answer) directly in the main function, I am able to serve all the files, and the file within the inner directory too.

However, when I call the same within the handle function, it doesn't serve anything. I am guessing this has something to do with the path?

The problem can be in the file path you are requesting. It is prefixed with /dlpath/. You must strip this prefix and pass the rest of the text as a path. See: https://godoc.org/net/http#FileServer

Could you try this code snippet:

package main

import (
    "log"
    "net/http"
)

func main() {
        dirName := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve"

        http.Handle("/dlpath/", http.StripPrefix("/dlpath", http.FileServer(http.Dir(dirName))))

        err := http.ListenAndServe(":8001", nil)
        if err != nil {
                log.Fatal("ListenAndServe: ", err)
        }
}

Hope this helps.

P.S. This is from the case when you are serving the directory in the handler function.

The accepted answer and some amount of googling helped me write FileServer with a different handler (http.HandleFunc). Using HandleFunc allowed me to set up other things like authentication etc.,

The key was to call ServeHTTP()

So, the handler function can be modified in the following manner to serve all the files in a directory:

func handle(writer http.ResponseWriter, r *http.Request) {
    {
        dirName := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve"
        http.StripPrefix("/dlpath", http.FileServer(http.Dir(dirName))).ServeHTTP(writer, r)
    }
}