golang简单文件服务器

文件服务器,顾名思义,就是提供远程文件目录浏览以及下载的服务。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
"log"
"net/http"
)

func main() {
workPath := "./"
mux := http.NewServeMux()
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(workPath))))

server := &http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}

log.Printf("Server Running At http://%v\n", server.Addr)
server.ListenAndServe()
}

使用

1
go run main.go