youbbs avatar

Go 的两个web 框架推荐

🕤 by youbbs

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

Go:
1
2
3
4
5
6
7
8
9
10
11
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/

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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()
}

💘 相关文章

评论

共1条关于"Go 的两个web 框架推荐"的评论

youbbs avatar
#1 youbbs 回复

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

Go:
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
32
33
34
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()

 }

写一条评论

Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 1ms