GolangNote

Golang笔记

goji 分组路由的例子

Permalink

goji 分组路由的例子

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
package main

import (
	"fmt"
	"net/http"

	"goji.io"
	"goji.io/pat"
)

func hello(w http.ResponseWriter, r *http.Request) {
	name := pat.Param(r, "name")
	fmt.Fprintf(w, "Hello, %s!", name)
}

func main() {

	root := goji.NewMux()

	users := goji.SubMux()
	root.Handle(pat.New("/user/*"), users)

	albums := goji.SubMux()
	root.Handle(pat.New("/album/*"), albums)

	// e.g., GET /user/carl
	users.HandleFunc(pat.Get("/:name"), hello)
	// e.g., GET /album/aswq
	albums.HandleFunc(pat.Get("/:name"), hello)

	http.ListenAndServe("localhost:8000", root)
}

Related articles

关掉goji 的log 输出

goji 默认对每个请求都输出请求时间,在项目刚起步时可以作为性能参考,但在实际生产环境里,可以关掉log...

Write a Comment to "goji 分组路由的例子"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 0ms