注意:本函式库原来自于github.com/codegangsta/negroni‐‐ Github会自动将连线转到本连结, 但我们建议你更新一下参照.
在Go语言里,Negroni 是一个很地道的 web 中间件,它是微型,非嵌入式,并鼓励使用原生net/http处理器的库。
如果你用过并喜欢Martini框架,但又不想框架中有太多魔幻性的特征,那 Negroni 就是你的菜了,相信它非常适合你。
语言翻译:
German ﴾de_DE﴿
Português Brasileiro ﴾pt_BR﴿
简体中文 ﴾zh_cn﴿
繁體中文 ﴾zh_tw﴿
日本語 ﴾ja_JP﴿
入门指导
当安装了 Go 语言并设置好了GOPATH后,新建你第一个.go文件,我们叫它server.go吧。
packagemain
import(
"github.com/urfave/negroni"
"net/http"
"fmt"
)
funcmain(){
mux:=http.NewServeMux()
mux.HandleFunc("/",func(whttp.ResponseWriter,req*http.Request){
fmt.Fprintf(w,"Welcometothehomepage!")
})
n:=negroni.Classic()
n.UseHandler(mux)
n.Run(":3000")
}
然后安装 Negroni 包(它依赖Go 1.1或更高的版本):
gogetgithub.com/urfave/negroni
然后运行刚建好的 server.go 文件:
gorunserver.go
这时一个 Gonet/httpWeb 服务器就跑在localhost:3000上,使用浏览器打开localhost:3000可以看到输出结果。
打包
如果negroni在Debian环境下是个套件, 可直接 执行aptinstallgolang‐github‐urfave‐negroni‐dev安装﴾这在sid仓库中﴿.
Negroni 是一个框架吗?
Negroni不是一个框架,它是为了方便使用net/http而设计的一个库而已。
路由呢?