为什么变量是nil,尽管我将对象的引用放在那里?

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

why variable is nil although I put there reference to object

问题

我无法弄清楚为什么在调用ConnectToMongo之后,变量session仍然为nil。如果ConnectToMongo接受的是非引用类型,比如ConnectToMongo(session mgo.Session),我可以理解这种情况,但是返回函数ConnectToMongo后,引用变量类型*mgo.Session必须被保存。

它的输出是:

nil. Why?

更新

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. )
  6. func ConnectToMongo(session **mgo.Session) {
  7. if session == nil {
  8. var err error
  9. *session, err = mgo.Dial("localhost:27028")
  10. if err != nil {
  11. panic(err)
  12. }
  13. }
  14. }
  15. func main() {
  16. var session *mgo.Session
  17. ConnectToMongo(&session)
  18. if session == nil {
  19. fmt.Println("nil. Why?")
  20. } else {
  21. fmt.Println("not nil. Ok.")
  22. }
  23. }

相同的输出:

  1. nil. Why?
英文:

I cannot figure out why after calling ConnectToMongo variable session is still nil. I would understand it if ConnectToMongo accepts not reference type like ConnectToMongo(session mgo.Session) but reference variable type *mgo.Session must be saved after returning function ConnectToMongo

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. )
  6. func ConnectToMongo(session *mgo.Session) {
  7. if session == nil {
  8. var err error
  9. session, err = mgo.Dial("localhost:27028")
  10. if err != nil {
  11. panic(err)
  12. }
  13. }
  14. }
  15. func main() {
  16. var session *mgo.Session
  17. ConnectToMongo(session)
  18. if session == nil {
  19. fmt.Println("nil. Why?")
  20. }
  21. }

It outputs:

nil. Why?

Update

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. )
  6. func ConnectToMongo(session **mgo.Session) {
  7. if session == nil {
  8. var err error
  9. *session, err = mgo.Dial("localhost:27028")
  10. if err != nil {
  11. panic(err)
  12. }
  13. }
  14. }
  15. func main() {
  16. var session *mgo.Session
  17. ConnectToMongo(&session)
  18. if session == nil {
  19. fmt.Println("nil. Why?")
  20. } else {
  21. fmt.Println("not nil. Ok.")
  22. }
  23. }

The same output:

  1. nil. Why?

答案1

得分: 2

你需要传递一个指向指针的指针来存储指针的值。否则,你只是将指针的值复制给ConnectToMongo函数。

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. )
  6. func ConnectToMongo(session **mgo.Session) {
  7. if *session == nil {
  8. var err error
  9. *session, err = mgo.Dial("localhost:27028")
  10. if err != nil {
  11. panic(err)
  12. }
  13. }
  14. }
  15. func main() {
  16. var session *mgo.Session
  17. ConnectToMongo(&session)
  18. if session == nil {
  19. fmt.Println("nil. Why?")
  20. }
  21. }
英文:

You need to pass a pointer to pointer to store the value of the pointer. Otherwise your are copying the value of the pointer to the ConnectToMongo function.

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. )
  6. func ConnectToMongo(session **mgo.Session) {
  7. if *session == nil {
  8. var err error
  9. *session, err = mgo.Dial("localhost:27028")
  10. if err != nil {
  11. panic(err)
  12. }
  13. }
  14. }
  15. func main() {
  16. var session *mgo.Session
  17. ConnectToMongo(&session)
  18. if session == nil {
  19. fmt.Println("nil. Why?")
  20. }
  21. }

huangapple
  • 本文由 发表于 2015年1月16日 10:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/27976275.html
匿名

发表评论

匿名网友

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

确定