GolangNote

Golang笔记

goji 分组路由的例子

Permalink

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
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)
}

本文网址: https://golangnote.com/topic/178.html 转摘请注明来源

Related articles

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

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