对于web开发者来收, 需要快速搭建一个web服务, 之前都在使用 nodejs实现的 http-server
今天发现golang可以30行代码搞定类似http-server的功能, golang着实强大.

简单说明, 和代码

flag包处理命令行的参数, fmt包不多说, net/http负责http的服务 log负责打印点日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

package main

import (
"flag"
"fmt"
"log"
"net/http"
)

var (
port int
host string
path string
)

func init() {
flag.IntVar(&port, "port", 8000, "server port listen")
flag.StringVar(&host, "host", "0.0.0.0", "host ip address")
flag.StringVar(&path, "path", "./", "server document root")
flag.Parse()
}

func main() {
log.Printf("server listen at [%v]:[%v] and document root is [%v]\n", host, port, path)
log.Println(path)
fs := http.FileServer(http.Dir(path))
http.Handle("/", fs)
http.ListenAndServe(fmt.Sprintf("%v:%v", host, port), nil)
}

使用

查看帮助 gohttpserver.exe -h

1
2
3
4
5
6
7
8
$ gohttpserver.exe -h
Usage of gohttpserver.exe:
-host string
host ip address (default "0.0.0.0")
-path string
server document root (default "./")
-port int
server port listen (default 8000)

启动 gohttpserver.exe --host=127.0.0.1 --port=9999 --path=d:/workspaces

有问题, 欢迎拍砖