我是一名nodejs开发人员,我通常使用我的应用程序的结构,该结构包含一个配置包/对象,其中包含对我经常使用的库和配置选项的引用。通常,此配置对象也保存我的数据库连接,并且可以通过我的应用程序访问它。
我尝试在go中构建类似于此并且失败可怕。
我的计划是构建一个公共变量,它保存我的config结构的引用。但当我试着打电话给我ItemContainerGenerator
时,我感到恐慌:
DataGrid
这是我的配置文件。正如您所看到的,我使用Header
进行redis连接。
configure.go
DataGridRow
这是我使用Config.Database
的地方。
handlers.go
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
这样的功能:
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
}
我更改了处理程序
的用法handlers.go
Config.Database
......一切都开始完美。
我的问题是我根本不明白为什么......当我使用公共指针时为什么会出现这种恐慌?为什么当我从函数返回相同的指针时我没有得到? / p>
答案 0 :(得分:4)
Config := &Configuration{conn}
此行会创建一个新的本地变量Config
,因此全局Config
永远不会设置为任何内容。放弃:
以不创建新变量,而是使用全局Config
。
答案 1 :(得分:1)
您遇到此问题是因为您的Config
全局变量未设置或仍保持nil值。正如错误所说:
运行时错误:无效的内存地址或无指针取消引用
确保你打过电话
在func Configure()
函数
main
和tkausl回答在{strong> configure.go 文件中将:
redis分配给全局变量时,请删除conn
:< / p>
Config = &Configuration{conn}