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?
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.
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 answer remove the :
when you assign the conn
redis to global variable to be like this in your configure.go file:
Config = &Configuration{conn}