mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
修正文案中的 wildCard -> wildChild 字段,图上画错的 wildCard 之后也需要修改
This commit is contained in:
parent
87012f4e51
commit
f153283072
@ -156,7 +156,7 @@ radix 的节点类型为 `*httprouter.node`,为了说明方便,我们留下
|
||||
```
|
||||
path: 当前节点对应的路径中的字符串
|
||||
|
||||
wildCard: 子节点是否为参数节点,即 wildCard node,或者说 :id 这种类型的节点
|
||||
wildChild: 子节点是否为参数节点,即 wildcard node,或者说 :id 这种类型的节点
|
||||
|
||||
nType: 当前节点类型,有四个枚举值: 分别为 static/root/param/catchAll。
|
||||
static // 非根节点的普通字符串节点
|
||||
@ -164,7 +164,8 @@ nType: 当前节点类型,有四个枚举值: 分别为 static/root/param/catc
|
||||
param // 参数节点,例如 :id
|
||||
catchAll // 通配符节点,例如 *anyway
|
||||
|
||||
indices: 子节点索引,当子节点为非参数类型,即本节点的 wildCard 为 false 时,会将每个子节点的首字母放在该索引数组。说是数组,实际上是个 string。
|
||||
indices: 子节点索引,当子节点为非参数类型,即本节点的 wildChild 为 false 时,会将每个子节点的首字母放在该索引数组。说是数组,实际上是个 string。
|
||||
|
||||
```
|
||||
当然,PUT 路由只有唯一的一条路径。接下来,我们以后续的多条 GET 路径为例,讲解子节点的插入过程。
|
||||
|
||||
@ -194,14 +195,15 @@ indices: 子节点索引,当子节点为非参数类型,即本节点的 wild
|
||||

|
||||
|
||||
### 子节点冲突处理
|
||||
|
||||
在路由本身只有字符串的情况下,不会发生任何冲突。只有当路由中含有 wildcard(类似 :id) 或者 catchAll 的情况下才可能冲突。这一点在前面已经提到了。
|
||||
|
||||
子节点的冲突处理很简单,分几种情况:
|
||||
|
||||
1. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildCard 被设置为 false。例如:`GET /user/getAll` 和 `GET /user/:id/getAddr`,或者 `GET /user/*aaa` 和 `GET /user/:id`。
|
||||
2. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildCard 被设置为 true,但该父节点的 wildCard 子节点要插入的 wildCard 名字不一样。例如:`GET /user/:id/info` 和 `GET /user/:name/info`。
|
||||
1. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildChild 被设置为 false。例如:`GET /user/getAll` 和 `GET /user/:id/getAddr`,或者 `GET /user/*aaa` 和 `GET /user/:id`。
|
||||
2. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildChild 被设置为 true,但该父节点的 wildcard 子节点要插入的 wildcard 名字不一样。例如:`GET /user/:id/info` 和 `GET /user/:name/info`。
|
||||
3. 在插入 catchAll 节点时,父节点的 children 非空。例如:`GET /src/abc` 和 `GET /src/*filename`,或者 `GET /src/:id` 和 `GET /src/*filename`。
|
||||
4. 在插入 static 节点时,父节点的 wildCard 字段被设置为 true。
|
||||
4. 在插入 static 节点时,父节点的 wildChild 字段被设置为 true。
|
||||
5. 在插入 static 节点时,父节点的 children 非空,且子节点 nType 为 catchAll。
|
||||
|
||||
只要发生冲突,都会在初始化的时候 panic。
|
||||
|
Loading…
x
Reference in New Issue
Block a user