运行时错误:公共指针中存在无效的内存地址或空指针解引用。

huangapple go评论104阅读模式
英文:

runtime error: invalid memory address or nil pointer dereference in public pointer

问题

我是一个Node.js开发者,通常在我的应用程序中使用一种结构,其中包含一个配置包/对象,其中包含对我经常使用的库和配置选项的引用。通常,这个配置对象也包含我的数据库连接,并且可以通过我的应用程序访问。

我尝试在Go中构建类似的结构,但失败了。

我的计划是构建一个公共变量,它持有对我的配置结构的引用。但是,当我尝试调用Config.Database时,我得到了以下错误:

  1. 2017/02/19 14:05:44 http: panic serving 127.0.0.1:53554: runtime error: invalid memory address or nil pointer dereference
  2. goroutine 50 [running]:
  3. net/http.(*conn).serve.func1(0xc42027c000)
  4. /usr/local/go/src/net/http/server.go:1491 +0x12a
  5. panic(0x9f45c0, 0xc42000c100)
  6. /usr/local/go/src/runtime/panic.go:458 +0x243
  7. main.SignUp(0xc4202780e0)
  8. /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
  9. ...

这是我的配置文件。你可以看到我在这里使用了tideland/golib/redis来进行Redis连接。

configure.go

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/tideland/golib/redis"
  7. )
  8. /*Configuration是应用程序配置的主要类型*/
  9. type Configuration struct {
  10. Database *redis.Connection
  11. }
  12. /*Config是应用程序配置的持有者*/
  13. var Config *Configuration
  14. /*Configure处理数据库连接*/
  15. func Configure() (*Configuration, error) {
  16. db, err := redis.Open(redis.TcpConnection("127.0.0.1:6379", 30*time.Second))
  17. if err != nil {
  18. fmt.Printf("Database connection error")
  19. return nil, err
  20. }
  21. conn, err := db.Connection()
  22. n, _ := conn.DoInt("INCR", "IDIDID")
  23. fmt.Printf(strconv.Itoa(n))
  24. if err != nil {
  25. fmt.Printf("Database connection error")
  26. return nil, err
  27. }
  28. /*Config是主要的配置对象*/
  29. Config := &Configuration{conn}
  30. return Config, err
  31. }

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

handlers.go

  1. func SignUp(ctx *iris.Context) {
  2. mail := ctx.FormValue("email")
  3. password := ctx.FormValue("password")
  4. passwordConfirm := ctx.FormValue("password-confirm")
  5. if password != passwordConfirm {
  6. ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"passwords dont match"})
  7. } else {
  8. user := User{mail, password, 0}
  9. db := Config.Database
  10. userID, err := db.DoInt("INCR", "HELLOIRIS:ID")
  11. if err != nil {
  12. ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"passwords dont match"})
  13. } else {
  14. user.ID = userID
  15. fmt.Printf("SAVED")
  16. ctx.Render("signup-success.html", nil)
  17. }
  18. ctx.JSON(200, user)
  19. }
  20. }

在这次失败之后,我将Configure函数更改如下:

configure.go

  1. func Configure() (Config *Configuration, err error) {
  2. if Config != nil {
  3. return
  4. }
  5. }

并且我在处理程序中更改了使用方式。

handlers.go

  1. config, err := Configure()
  2. if err != nil {
  3. ctx.RenderWithStatus(iris.StatusBadRequest, "400.html", ErrorPageData{"try again later"})
  4. return
  5. }
  6. user := User{mail, password, 0}
  7. db := config.Database
  8. ... 然后一切都开始完美地工作了
  9. 我的问题是我根本不明白为什么...为什么我在使用公共指针时会出现恐慌而在从函数返回相同指针时却没有出现恐慌
  10. <details>
  11. <summary>英文:</summary>
  12. I&#39;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&#39;s reachable trough my application.
  13. I tried to build similar to this in go and failed horribly.
  14. 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:
  15. 2017/02/19 14:05:44 http: panic serving 127.0.0.1:53554: runtime error: invalid memory address or nil pointer dereference
  16. goroutine 50 [running]:
  17. net/http.(*conn).serve.func1(0xc42027c000)
  18. /usr/local/go/src/net/http/server.go:1491 +0x12a
  19. panic(0x9f45c0, 0xc42000c100)
  20. /usr/local/go/src/runtime/panic.go:458 +0x243
  21. main.SignUp(0xc4202780e0)
  22. /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
  23. github.com/kataras/iris.HandlerFunc.Serve(0xafaf00, 0xc4202780e0)
  24. /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:211 +0x30
  25. github.com/kataras/iris.(*Context).Do(0xc4202780e0)
  26. /home/attila/dev/gopath/src/github.com/kataras/iris/context.go:152 +0x4d
  27. github.com/kataras/iris.(*serveMux).BuildHandler.func1(0xc4202780e0)
  28. /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:1059 +0x6ea
  29. github.com/kataras/iris.(*Framework).Build.func1.1(0xd87e20, 0xc4202701a0, 0xc420284000)
  30. /home/attila/dev/gopath/src/github.com/kataras/iris/iris.go:411 +0x72
  31. net/http.HandlerFunc.ServeHTTP(0xc420235080, 0xd87e20, 0xc4202701a0, 0xc420284000)
  32. /usr/local/go/src/net/http/server.go:1726 +0x44
  33. net/http.serverHandler.ServeHTTP(0xc420089f80, 0xd87e20, 0xc4202701a0, 0xc420284000)
  34. /usr/local/go/src/net/http/server.go:2202 +0x7d
  35. net/http.(*conn).serve(0xc42027c000, 0xd88820, 0xc42015c200)
  36. /usr/local/go/src/net/http/server.go:1579 +0x4b7
  37. created by net/http.(*Server).Serve
  38. /usr/local/go/src/net/http/server.go:2293 +0x44d
  39. 2017/02/19 14:05:44 http: panic serving 127.0.0.1:53560: runtime error: invalid memory address or nil pointer dereference
  40. goroutine 51 [running]:
  41. net/http.(*conn).serve.func1(0xc42027c180)
  42. /usr/local/go/src/net/http/server.go:1491 +0x12a
  43. panic(0x9f45c0, 0xc42000c100)
  44. /usr/local/go/src/runtime/panic.go:458 +0x243
  45. main.SignUp(0xc4202ac070)
  46. /home/attila/dev/gopath/src/github.com/attilasatan/helloiris/handlers.go:31 +0x258
  47. github.com/kataras/iris.HandlerFunc.Serve(0xafaf00, 0xc4202ac070)
  48. /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:211 +0x30
  49. github.com/kataras/iris.(*Context).Do(0xc4202ac070)
  50. /home/attila/dev/gopath/src/github.com/kataras/iris/context.go:152 +0x4d
  51. github.com/kataras/iris.(*serveMux).BuildHandler.func1(0xc4202ac070)
  52. /home/attila/dev/gopath/src/github.com/kataras/iris/http.go:1059 +0x6ea
  53. github.com/kataras/iris.(*Framework).Build.func1.1(0xd87e20, 0xc4202a60d0, 0xc4202840f0)
  54. /home/attila/dev/gopath/src/github.com/kataras/iris/iris.go:411 +0x72
  55. net/http.HandlerFunc.ServeHTTP(0xc420235080, 0xd87e20, 0xc4202a60d0, 0xc4202840f0)
  56. /usr/local/go/src/net/http/server.go:1726 +0x44
  57. net/http.serverHandler.ServeHTTP(0xc420089f80, 0xd87e20, 0xc4202a60d0, 0xc4202840f0)
  58. /usr/local/go/src/net/http/server.go:2202 +0x7d
  59. net/http.(*conn).serve(0xc42027c180, 0xd88820, 0xc42015c480)
  60. /usr/local/go/src/net/http/server.go:1579 +0x4b7
  61. created by net/http.(*Server).Serve
  62. Here is my configure file. As you can see I&#39;m using `tideland/golib/redis` for redis connection.
  63. **configure.go**
  64. package main
  65. import (
  66. &quot;fmt&quot;
  67. &quot;strconv&quot;
  68. &quot;time&quot;
  69. &quot;github.com/tideland/golib/redis&quot;
  70. )
  71. /*Configuration is the main type of app configuration */
  72. type Configuration struct {
  73. Database *redis.Connection
  74. }
  75. /*Config is app configuration holder */
  76. var Config *Configuration
  77. /*Configure handles database connection */
  78. func Configure() (*Configuration, error) {
  79. db, err := redis.Open(redis.TcpConnection(&quot;127.0.0.1:6379&quot;, 30*time.Second))
  80. if err != nil {
  81. fmt.Printf(&quot;Database connection error&quot;)
  82. return nil, err
  83. }
  84. conn, err := db.Connection()
  85. n, _ := conn.DoInt(&quot;INCR&quot;, &quot;IDIDID&quot;)
  86. fmt.Printf(strconv.Itoa(n))
  87. if err != nil {
  88. fmt.Printf(&quot;Database connection error&quot;)
  89. return nil, err
  90. }
  91. /*Config is the main configuration object*/
  92. Config := &amp;Configuration{conn}
  93. return Config, err
  94. }
  95. And here is where I use `Config.Database`.
  96. **handlers.go**
  97. func SignUp(ctx *iris.Context) {
  98. mail := ctx.FormValue(&quot;email&quot;)
  99. password := ctx.FormValue(&quot;password&quot;)
  100. passwordConfirm := ctx.FormValue(&quot;password-confirm&quot;)
  101. if password != passwordConfirm {
  102. ctx.RenderWithStatus(iris.StatusBadRequest, &quot;400.html&quot;, ErrorPageData{&quot;passwords dont match&quot;})
  103. } else {
  104. user := User{mail, password, 0}
  105. db := Config.Database
  106. userID, err := db.DoInt(&quot;INCR&quot;, &quot;HELLOIRIS:ID&quot;)
  107. if err != nil {
  108. ctx.RenderWithStatus(iris.StatusBadRequest, &quot;400.html&quot;, ErrorPageData{&quot;passwords dont match&quot;})
  109. } else {
  110. user.ID = userID
  111. fmt.Printf(&quot;SAVED&quot;)
  112. ctx.Render(&quot;signup-success.html&quot;, nil)
  113. }
  114. ctx.JSON(200, user)
  115. }
  116. }
  117. After this fail I changed `Configure` function like this:
  118. **configure.go**
  119. func Configure() (Config *Configuration, err error) {
  120. if Config != nil {
  121. return
  122. }
  123. }
  124. and I changed the usage at handler
  125. **handlers.go**
  126. config, err := Configure()
  127. if err != nil {
  128. ctx.RenderWithStatus(iris.StatusBadRequest, &quot;400.html&quot;, ErrorPageData{&quot;try again later&quot;})
  129. return
  130. }
  131. user := User{mail, password, 0}
  132. db := config.Database
  133. ... and everything start to work perfectly.
  134. My problem is I simply don&#39;t understand why... Why did I get this panic when I use a public pointer and why didn&#39;t I get when I returned the same pointer from a function?
  135. </details>
  136. # 答案1
  137. **得分**: 4
  138. 这行代码创建了一个新的本地变量`Config`所以全局的`Config`变量没有被赋值去掉冒号就可以不创建新的变量而是使用全局的`Config`变量
  139. <details>
  140. <summary>英文:</summary>
  141. Config := &amp;Configuration{conn}
  142. 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.
  143. </details>
  144. # 答案2
  145. **得分**: 1
  146. 你遇到这个问题是因为你的`Config`全局变量没有设置或者仍然保持为nil正如错误所说
  147. &gt; 运行时错误无效的内存地址或nil指针解引用
  148. 请确保在你的`main`函数中调用`func Configure()`之前调用了处理程序
  149. 并且根据[tkausl][1]的回答在你的**configure.go**文件中`conn` redis分配给全局变量时删除`:`使其变为以下形式
  150. Config = &amp;Configuration{conn}
  151. [1]: https://stackoverflow.com/a/42326810/2652524
  152. <details>
  153. <summary>英文:</summary>
  154. You&#39;re getting this problem because your `Config` global variable is not set or still hold nil value. as the error said :
  155. &gt; runtime error: invalid memory address or nil pointer dereference
  156. Make sure you called
  157. `func Configure()` before the handler called in your `main` function
  158. 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:
  159. Config = &amp;Configuration{conn}
  160. [1]: https://stackoverflow.com/a/42326810/2652524
  161. </details>

huangapple
  • 本文由 发表于 2017年2月19日 19:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42326753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定