使用 beego 验证码:无效的内存地址或 nil 指针取消引用

Using beego captcha: invalid memory address or nil pointer dereference

我想在beego下使用captcha生成验证码。但是它有错误无效的内存地址或零指针取消引用。有谁知道如何解决这个问题?谢谢。

Request Method: GET
Request URL:    /accounts/forgotpassword
RemoteAddr: 127.0.0.1
Stack
C:/Go/src/runtime/asm_amd64.s:573
C:/Go/src/runtime/panic.go:505
C:/Go/src/text/template/exec.go:137
C:/Go/src/runtime/asm_amd64.s:573
C:/Go/src/runtime/panic.go:505
C:/Go/src/runtime/panic.go:63
C:/Go/src/runtime/signal_windows.go:167
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/utils/captcha/captcha.go:186
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/utils/captcha/captcha.go:164
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/utils/captcha/captcha.go:267
C:/Go/src/runtime/asm_amd64.s:573
C:/Go/src/reflect/value.go:447
C:/Go/src/reflect/value.go:308
C:/Go/src/text/template/exec.go:667
C:/Go/src/text/template/exec.go:535
C:/Go/src/text/template/exec.go:432
C:/Go/src/text/template/exec.go:405
C:/Go/src/text/template/exec.go:231
C:/Go/src/text/template/exec.go:239
C:/Go/src/text/template/exec.go:194
C:/Go/src/text/template/exec.go:177
C:/Go/src/html/template/template.go:137
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/template.go:66
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/controller.go:283
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/controller.go:234
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/controller.go:214
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/router.go:863
C:/Go/src/net/http/server.go:2694
C:/Go/src/net/http/server.go:1830
C:/Go/src/runtime/asm_amd64.s:2361

我的代码: conf\app.conf

# Cache Provider
CacheProvider = redis
CacheConnection = {"conn":"127.0.0.1:6379"}

controllers\main.go

package controllers

import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
  "github.com/astaxie/beego/utils/captcha"
)


var(
    cpt *captcha.Captcha
    CacheProvider string = beego.AppConfig.String("CacheProvider")
    CacheConnection string = beego.AppConfig.String("CacheConnection")
)


func init() {
  store, _ := cache.NewCache(CacheProvider, CacheConnection)
  cpt = captcha.NewWithFilter("/accounts/captca/", store)
}

views\forgotpasswordcontroller\get.tpl

<div class="w3-container w3-center">
      <form method="post" id="mainForm"class="w3-container" style="margin-top:90px">
        <div class="w3-card " style="    padding-left: 0px;
        padding-right: 0px;    margin-top: 30px;">
            <div class="w3-container">
                <h1>Reset password</h1>
            </div><div class="w3-container" style="    padding-bottom: 16px;">
            {{create_captcha}}
                <input type="text"  class="w3-input   "name="captcha"style="outline: none;">
            <p style="text-align: left;margin-top: 0px;color:red">
            {{if .Errors.Captcha}}
                {{.Errors.Captcha}}{{else}}&zwnj;{{end}}</p>
                <input type="submit" value="Request reset password" onclick="login()" class="w3-button w3-indigo w3-block w3-round-large">
            </div>
        </div>
      </form>
  </div>

controllers\forgotpassword.go

package controllers

import (
  "github.com/astaxie/beego"
)


type ForgotPasswordController struct {
  beego.Controller
}


func (c *ForgotPasswordController) Get() {
  beego.Debug("In ForgotPasswordController:Get - Start")
  c.Layout = "shared/layout.tpl"
}//end ForgotPasswordController:Get()


func (this *ForgotPasswordController) Post() {

  beego.Debug("In ForgotPasswordController:Post - Start")

  captchaVerification := cpt.VerifyReq(this.Ctx.Request)

  if !captchaVerification {
    errormap := make(map[string]string)
    beego.Debug("In ForgotPasswordController:Post - captchaVerification Got wrong captcha")
    errormap["Captcha"] = "Sorry but the characters you endered didn't match. Please try again"
    this.Data["Errors"] = errormap
    return
  }

} //end ForgotPassword() func

环境

刚刚在我的本地测试了你的代码。错误来自缓存创建部分。

store, err := cache.NewCache(CacheProvider, CacheConnection)
if err != nil {
    log.Fatal(err.Error())
    os.Exit(0)
}

要获取详细错误,请检查从 cache.NewCache() 返回的 err 变量。此外,最好始终记录来自错误对象的任何可能错误,不要忽略它。

这是错误日志:

2018/11/14 11:13:24 cache: unknown adapter name "redis" (forgot to import?)

上面的错误是因为缓存包找不到 redis 适配器。那是因为你还没有导入包。那么让我们尝试导入它,那么你的问题就解决了。

import (
    "fmt"
    "log"
    "os"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/cache"
    "github.com/astaxie/beego/utils/captcha"

    _ "github.com/astaxie/beego/cache/redis" // <----- this one
)

由于我们不直接与缓存redis包交互,所以用_导入。