草庐IT

pointers - 运行时错误 : invalid memory address or nil pointer dereference in public pointer

coder 2023-06-28 原文

我是一名 nodejs 开发人员,我通常为我的应用程序使用一个结构,该结构包含一个配置包/对象,该对象包含对我常用的库和配置选项的引用。通常,此配置对象也包含我的数据库连接,并且可以通过我的应用程序访问它。

我试图在 go 中构建与此类似的东西,但失败得很惨。

我的计划是构建一个公共(public)变量,它包含对我的配置结构的引用。但是当我尝试调用我的 Config.Database 时,我感到 panic :

2017/02/19 14:05:44 http: panic serving 127.0.0.1:53554: runtime error: invalid memory address or nil pointer dereference
    goroutine 50 [running]:
    net/http.(*conn).serve.func1(0xc42027c000)
        /usr/local/go/src/net/http/server.go:1491 +0x12a
    panic(0x9f45c0, 0xc42000c100)
        /usr/local/go/src/runtime/panic.go:458 +0x243
    main.SignUp(0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
    github.com/kataras/iris.HandlerFunc.Serve(0xafaf00, 0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:211 +0x30
    github.com/kataras/iris.(*Context).Do(0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/context.go:152 +0x4d
    github.com/kataras/iris.(*serveMux).BuildHandler.func1(0xc4202780e0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:1059 +0x6ea
    github.com/kataras/iris.(*Framework).Build.func1.1(0xd87e20, 0xc4202701a0, 0xc420284000)
        /home/attila/dev/gopath/src/github.com/kataras/iris/iris.go:411 +0x72
    net/http.HandlerFunc.ServeHTTP(0xc420235080, 0xd87e20, 0xc4202701a0, 0xc420284000)
        /usr/local/go/src/net/http/server.go:1726 +0x44
    net/http.serverHandler.ServeHTTP(0xc420089f80, 0xd87e20, 0xc4202701a0, 0xc420284000)
        /usr/local/go/src/net/http/server.go:2202 +0x7d
    net/http.(*conn).serve(0xc42027c000, 0xd88820, 0xc42015c200)
        /usr/local/go/src/net/http/server.go:1579 +0x4b7
    created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2293 +0x44d
    2017/02/19 14:05:44 http: panic serving 127.0.0.1:53560: runtime error: invalid memory address or nil pointer dereference
    goroutine 51 [running]:
    net/http.(*conn).serve.func1(0xc42027c180)
        /usr/local/go/src/net/http/server.go:1491 +0x12a
    panic(0x9f45c0, 0xc42000c100)
        /usr/local/go/src/runtime/panic.go:458 +0x243
    main.SignUp(0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
    github.com/kataras/iris.HandlerFunc.Serve(0xafaf00, 0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:211 +0x30
    github.com/kataras/iris.(*Context).Do(0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/kataras/iris/context.go:152 +0x4d
    github.com/kataras/iris.(*serveMux).BuildHandler.func1(0xc4202ac070)
        /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:1059 +0x6ea
    github.com/kataras/iris.(*Framework).Build.func1.1(0xd87e20, 0xc4202a60d0, 0xc4202840f0)
        /home/attila/dev/gopath/src/github.com/kataras/iris/iris.go:411 +0x72
    net/http.HandlerFunc.ServeHTTP(0xc420235080, 0xd87e20, 0xc4202a60d0, 0xc4202840f0)
        /usr/local/go/src/net/http/server.go:1726 +0x44
    net/http.serverHandler.ServeHTTP(0xc420089f80, 0xd87e20, 0xc4202a60d0, 0xc4202840f0)
        /usr/local/go/src/net/http/server.go:2202 +0x7d
    net/http.(*conn).serve(0xc42027c180, 0xd88820, 0xc42015c480)
        /usr/local/go/src/net/http/server.go:1579 +0x4b7
    created by net/http.(*Server).Serve

这是我的配置文件。如您所见,我正在使用 tideland/golib/redis 进行 redis 连接。

configure.go

package main

import (
    "fmt"
    "strconv"
    "time"

    "github.com/tideland/golib/redis"
)

/*Configuration is the main type of app configuration */
type Configuration struct {
    Database *redis.Connection
}

/*Config is app configuration holder */
var Config *Configuration

/*Configure handles database connection */
func Configure() (*Configuration, error) {

    db, err := redis.Open(redis.TcpConnection("127.0.0.1:6379", 30*time.Second))

    if err != nil {
        fmt.Printf("Database connection error")
        return nil, err
    }
    conn, err := db.Connection()

    n, _ := conn.DoInt("INCR", "IDIDID")

    fmt.Printf(strconv.Itoa(n))

    if err != nil {
        fmt.Printf("Database connection error")
        return nil, err
    }

    /*Config is the main configuration object*/

    Config := &Configuration{conn}

    return Config, err
}

这里是我使用 Config.Database 的地方。

handlers.go

func SignUp(ctx *iris.Context) {
    mail := ctx.FormValue("email")
    password := ctx.FormValue("password")
    passwordConfirm := ctx.FormValue("password-confirm")

    if password != passwordConfirm {
        ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"passwords dont match"})
    } else {

        user := User{mail, password, 0}
        db := Config.Database

        userID, err := db.DoInt("INCR", "HELLOIRIS:ID")

        if err != nil {
            ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"passwords dont match"})
        } else {

            user.ID = userID

            fmt.Printf("SAVED")

            ctx.Render("signup-success.html", nil)
        }
        ctx.JSON(200, user)
    }
}

在这次失败之后,我改变了 Configure 函数,如下所示:

configure.go

func Configure() (Config *Configuration, err error) {

    if Config != nil {
    return
    }
}

并且我更改了处理程序的用法

handlers.go

config, err := Configure()

if err != nil {
    ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"try again later"})
    return
}

user := User{mail, password, 0}
db := config.Database

...一切开始完美运行。

我的问题是我根本不明白为什么......为什么我在使用公共(public)指针时会出现这种 panic ,而当我从函数返回相同的指针时为什么不会出现这种情况?

最佳答案

Config := &Configuration{conn}

这一行创建了一个新的本地变量Config,所以全局Config永远不会被设置为任何东西。删除 : 以不创建新变量并改用全局 Config

关于pointers - 运行时错误 : invalid memory address or nil pointer dereference in public pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42326753/

有关pointers - 运行时错误 : invalid memory address or nil pointer dereference in public pointer的更多相关文章

随机推荐