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

golang web 框架 goji 介绍

goji 是个后起的golang web框架,避免其它golang web 框架走过的坑,在性能、简单性方面做得很好。...

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

Submit Comment Login
Based on Golang + fastHTTP + youdb | go1.16 Processed in 1ms