Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

znet/request.go 文件中Request对象index成员的初始值问题 #323

Open
hpp131 opened this issue Jun 6, 2024 · 1 comment
Open

Comments

@hpp131
Copy link

hpp131 commented Jun 6, 2024

Question: 为什么在NewRequest()方法中给index成员变量赋值为-1,同时在Request对象的RouterSlicesNext()方法中第一行r.index++, 意为在-1的基础上加一。但是如果直接在初始化时给int类型的默认值,那么在RouterSlicesNext()方法中就可以直接执行for循环,而不用额外执行r.index++语句。请问这样设计是出于什么目的吗?

func NewRequest(conn ziface.IConnection, msg ziface.IMessage) ziface.IRequest {
        ...
        ...
	req.index = -1
	return req
}
func (r *Request) RouterSlicesNext() {
	r.index++
	for r.index < int8(len(r.handlers)) {
		r.handlers[r.index](r)
		r.index++
	}
}
@YanHeDoki
Copy link
Collaborator

执行 r.index++ 是为了执行切片中的下一个函数,路由开始执行就调用 RouterSlicesNext() 从0开始,但执行到中间的函数,例如第二个函数的调用 RouterSlicesNext() 是期望执行第三个函数,需要去 r.index++,也就导致了赋值需要为 -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants