youbbs
youbbs
4409 1 0

Go 的两个web 框架推荐

martini 轻量,推荐
http://martini.codegangsta.io/
https://github.com/go-martini/martini

package main

import "github.com/go-martini/martini"

func main() {
  m := martini.Classic()
  m.Get("/", func() string {
    return "Hello world!"
  })
  m.Run()
}

Beego Framework 国产,较重
http://beego.me/
https://github.com/astaxie/beego/

package main

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {
    this.Ctx.WriteString("hello world")
}

func main() {
    beego.Router("/", &MainController{})
    beego.Run()
}
0

See Also

Nearby


Discussion (1)

youbbs
youbbs 2015-06-06 01:29

这个更快 goji https://github.com/zenazn/goji

package main

 import (
         "flag"
         "fmt"
         "github.com/zenazn/goji"
         "github.com/zenazn/goji/web"
         "net/http"
 )

 func Home(w http.ResponseWriter, r *http.Request) {
         w.Write([]byte("Hello, World!"))
 }

 func SayHello(c web.C, w http.ResponseWriter, r *http.Request) {
         // get the name from web.C
         // a request-local context object
         // see https://github.com/zenazn/goji/blob/master/web/web.go
         name := c.URLParams["name"]
         w.Write([]byte(fmt.Sprintf("Hello %s !", name)))
 }

 func main() {
         goji.Get("/", Home)
         goji.Get("/:name", SayHello)

         // goji default port number is 8000
         // change to port number 8080
         // with flag.Set
         flag.Set("bind", ":8080")

         goji.Serve()

 }
0
Login Topics