话不多说,代码见下面
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
)
func main() {
var httpport int
var webpath string
flag.IntVar(&httpport, "httpport", 80, "http服务器监听的端口号")
flag.StringVar(&webpath, "webpath", ".", "http服务器www目录")
flag.Parse()
if httpport <= 0 || httpport > 65535 {
fmt.Println("httpport参数错误")
flag.Usage()
return
}
fmt.Println("httpport:", httpport)
fmt.Println("webpath:", webpath)
http.Handle("/", http.FileServer(http.Dir(webpath)))
http.ListenAndServe(":"+strconv.Itoa(httpport), nil)
}文章作者:沃航科技