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

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

why variable is nil although I put there reference to object

问题

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

它的输出是:

nil. Why?

更新

package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
)

func ConnectToMongo(session **mgo.Session) {
	if session == nil {
		var err error
		*session, err = mgo.Dial("localhost:27028")
		if err != nil {
			panic(err)
		}
	}
}

func main() {
	var session *mgo.Session
	ConnectToMongo(&session)
	if session == nil {
		fmt.Println("nil. Why?")
	} else {
		fmt.Println("not nil. Ok.")
	}
}

相同的输出:

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

package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
)

func ConnectToMongo(session *mgo.Session) {
	if session == nil {
		var err error
		session, err = mgo.Dial("localhost:27028")
		if err != nil {
			panic(err)
		}
	}
}

func main() {
	var session *mgo.Session
	ConnectToMongo(session)
	if session == nil {
		fmt.Println("nil. Why?")
	}
}

It outputs:

nil. Why?

Update

package main

import (
	"fmt"
	"gopkg.in/mgo.v2"
)

func ConnectToMongo(session **mgo.Session) {
	if session == nil {
		var err error
		*session, err = mgo.Dial("localhost:27028")
		if err != nil {
			panic(err)
		}
	}
}

func main() {
	var session *mgo.Session
	ConnectToMongo(&session)
	if session == nil {
		fmt.Println("nil. Why?")
	} else {
		fmt.Println("not nil. Ok.")
	}
}

The same output:

nil. Why?

答案1

得分: 2

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

package main

import (
    "fmt"
    "gopkg.in/mgo.v2"
)

func ConnectToMongo(session **mgo.Session) {
    if *session == nil {
        var err error
        *session, err = mgo.Dial("localhost:27028")
        if err != nil {
            panic(err)
        }
    }
}

func main() {
    var session *mgo.Session
    ConnectToMongo(&session)
    if session == nil {
        fmt.Println("nil. Why?")
    }
}
英文:

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.

package main

import (
    "fmt"
    "gopkg.in/mgo.v2"
)

func ConnectToMongo(session **mgo.Session) {
    if *session == nil {
        var err error
        *session, err = mgo.Dial("localhost:27028")
        if err != nil {
            panic(err)
        }
    }
}

func main() {
    var session *mgo.Session
    ConnectToMongo(&session)
    if session == nil {
        fmt.Println("nil. Why?")
    }
}

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:

确定