golang beego router controller 호출 분석
2123 단어 GoLang
router add 방법은 controller Info 구조체에 값을 부여하는 controllerType 속성을 처리합니다
그 중 처리 코드는 다음과 같다beego/router.go
func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingMethods ...string) {
reflectVal := reflect.ValueOf(c)
t := reflect.Indirect(reflectVal).Type()
route := &controllerInfo{}
route.controllerType = t
}
저는 루터 컨트롤러만 보고 다른 코드는 다 지웠어요.
controller 처리
router ServeHTTP에서
go net/http Handler interface 다시 쓰기
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
그 중 처리 코드는 다음과 같다beego/router.go
func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// also defined runRouter & runMethod from filter
if !isRunnable {
//Invoke the request handler
vc := reflect.New(runRouter)
//
execController, ok := vc.Interface().(ControllerInterface)
if !ok {
panic("controller is not ControllerInterface")
}
if !context.ResponseWriter.Started {
switch runMethod {
default:
// beego ControllerInterface
// Controller HandlerFunc
if !execController.HandlerFunc(runMethod) {
var in []reflect.Value
//
method := vc.MethodByName(runMethod)
//
method.Call(in)
}
}
}
}
}
전체 반사 방법 테스트 코드 호출
package main
import (
"fmt"
"reflect"
)
type Xiao struct {
}
func (this Xiao) Xiaochuan() {
fmt.Println("this is xiao test")
}
func main() {
reflectVal := reflect.ValueOf(Xiao{})
t := reflect.Indirect(reflectVal).Type()
//
var in []reflect.Value
vc := reflect.New(t)
method := vc.MethodByName("Xiaochuan")
method.Call(in)
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Golang의 변수 메모리 분배와 탈출 분석bss: 초기화되지 않은 전역 변수를 저장하고, 정적 분배 4,stack: 창고, 주로 함수 호출 시 국부 변수를 저장한다.메모리는 시스템이 관리하고 창고에 쌓여 있는 방식을 통해 자동으로 분배되고 창고에 나가는 방...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.