title: runtime error: invalid memory address or nil pointer dereference in public pointer
body: <p>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.</p>
  
  <p>I tried to build similar to this in go and failed horribly. </p>
  
  <p>My plan was to build a public variable which holds a reference for my config struct. But when I try to call my <code>Config.Database</code> I get this panic:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here is my configure file. As you can see I'm using <code>tideland/golib/redis</code> for redis connection.</p>
  
  <p><strong>configure.go</strong></p>
  
  <pre><code>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 := &amp;Configuration{conn}
  
      return Config, err
  }
  </code></pre>
  
  <p>And here is where I use <code>Config.Database</code>. </p>
  
  <p><strong>handlers.go</strong></p>
  
  <pre><code>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)
      }
  }
  </code></pre>
  
  <p>After this fail I changed <code>Configure</code> function like this:</p>
  
  <p><strong>configure.go</strong></p>
  
  <pre><code>func Configure() (Config *Configuration, err error) {
  
      if Config != nil {
      return
      }
  }
  </code></pre>
  
  <p>and I changed the usage at handler  </p>
  
  <p><strong>handlers.go</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p>... and everything start to work perfectly.</p>
  
  <p>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?</p>