go语言的一个简单httpserver

httpserver.go

package main

import (
"net/http"
"flag"
"fmt"
"log"
) func main() {
var dir string
flag.StringVar(&dir, "d", ".", "web dir")
var port *int
port = flag.Int("p", 0, "port: must input")
var host string
flag.StringVar(&host,"h","","host")
flag.Parse()
if *port<=0 || *port>65535 {
flag.Usage()
return
} var addr = fmt.Sprintf("%s:%d", host,*port)
log.Printf("Listenning http://%s ...", addr) http.Handle("/", http.FileServer(http.Dir(dir)))
err := http.ListenAndServe(addr, nil)
if err!= nil {
log.Fatal("ERROR:",err)
}
}

  

$ go run httpserver.go -p 8080

上一篇:牛客网数据库SQL实战(11-15)


下一篇:JavaScript符串中每个单词的首字母大写化