英文:
runtime error: invalid memory address or nil pointer dereference in public pointer
问题
我是一个Node.js开发者,通常在我的应用程序中使用一种结构,其中包含一个配置包/对象,其中包含对我经常使用的库和配置选项的引用。通常,这个配置对象也包含我的数据库连接,并且可以通过我的应用程序访问。
我尝试在Go中构建类似的结构,但失败了。
我的计划是构建一个公共变量,它持有对我的配置结构的引用。但是,当我尝试调用Config.Database
时,我得到了以下错误:
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
...
这是我的配置文件。你可以看到我在这里使用了tideland/golib/redis
来进行Redis连接。
configure.go
package main
import (
"fmt"
"strconv"
"time"
"github.com/tideland/golib/redis"
)
/*Configuration是应用程序配置的主要类型*/
type Configuration struct {
Database *redis.Connection
}
/*Config是应用程序配置的持有者*/
var Config *Configuration
/*Configure处理数据库连接*/
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是主要的配置对象*/
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
... 然后一切都开始完美地工作了。
我的问题是我根本不明白为什么...为什么我在使用公共指针时会出现恐慌,而在从函数返回相同指针时却没有出现恐慌?
<details>
<summary>英文:</summary>
I'm a nodejs developer and I generally use a structure for my apps which holds a configuration package/object that holds references to my often used libs and configuration options. Generally this configuration object holds my database connections as well and it's reachable trough my application.
I tried to build similar to this in go and failed horribly.
My plan was to build a public variable which holds a reference for my config struct. But when I try to call my `Config.Database` I get this 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
Here is my configure file. As you can see I'm using `tideland/golib/redis` for redis connection.
**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
}
And here is where I use `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)
}
}
After this fail I changed `Configure` function like this:
**configure.go**
func Configure() (Config *Configuration, err error) {
if Config != nil {
return
}
}
and I changed the usage at handler
**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
... and everything start to work perfectly.
My problem is I simply don't understand why... Why did I get this panic when I use a public pointer and why didn't I get when I returned the same pointer from a function?
</details>
# 答案1
**得分**: 4
这行代码创建了一个新的本地变量`Config`,所以全局的`Config`变量没有被赋值。去掉冒号,就可以不创建新的变量,而是使用全局的`Config`变量。
<details>
<summary>英文:</summary>
Config := &Configuration{conn}
This line creates a new __local__ variable `Config` so the global `Config` is never set to anything. Drop the `:` to not create a new variable and use the global `Config` instead.
</details>
# 答案2
**得分**: 1
你遇到这个问题是因为你的`Config`全局变量没有设置或者仍然保持为nil值。正如错误所说:
> 运行时错误:无效的内存地址或nil指针解引用
请确保在你的`main`函数中调用`func Configure()`之前调用了处理程序。
并且根据[tkausl][1]的回答,在你的**configure.go**文件中,将`conn` redis分配给全局变量时删除`:`,使其变为以下形式:
Config = &Configuration{conn}
[1]: https://stackoverflow.com/a/42326810/2652524
<details>
<summary>英文:</summary>
You're getting this problem because your `Config` global variable is not set or still hold nil value. as the error said :
> runtime error: invalid memory address or nil pointer dereference
Make sure you called
`func Configure()` before the handler called in your `main` function
and as [tkausl][1] answer remove the `:` when you assign the `conn` redis to global variable to be like this in your **configure.go** file:
Config = &Configuration{conn}
[1]: https://stackoverflow.com/a/42326810/2652524
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论