我想在自定义函数中返回json响应。
我知道控制器文件中可能存在这种情况,但是我正在编写自定义中间件,该中间件在控制器外部工作。
我尝试了新的beego.controller,但这不起作用。
controllers / login.go
import (
"myapp/helpers/auth"
)
func (c *LoginController) Post() {
return auth.IsAdmin()
}
helplers / auth / isadmin.go
import (
"github.com/astaxie/beego"
)
func () IsAdmin(){
response := new beego.controller()
response.Data["json"] = map[string]string{"key":"value"}
response.ServeJSON()
response.StopRun()
}
IsAdmin函数返回错误:
运行时错误:无效的内存地址或nil指针取消引用
stilo:runtime error: invalid memory address or nil pointer dereference
Request Method: POST
Request URL: /login
RemoteAddr: ::1
Stack
/usr/local/go/src/runtime/panic.go:522
/usr/local/go/src/runtime/panic.go:82
/usr/local/go/src/runtime/signal_unix.go:390
/Users/burakdemirpolat/go/src/github.com/astaxie/beego/context/context.go:217
/Users/burakdemirpolat/go/src/stilo/helpers/token/token.go:25
/Users/burakdemirpolat/go/src/stilo/controllers/login.go:22
/usr/local/go/src/reflect/value.go:447
/usr/local/go/src/reflect/value.go:308
/Users/burakdemirpolat/go/src/github.com/astaxie/beego/router.go:853
/usr/local/go/src/net/http/server.go:2774
/usr/local/go/src/net/http/server.go:1878
/usr/local/go/src/runtime/asm_amd64.s:1337
因此,我想停止应用程序并返回类似PHP die方法的消息。 (在控制器外部)