--
Regards,
-Clark
The zero-value of a struct type is the struct with all its elements
set to their zero values.
Eg,
type T struct {
a int
b string
c *byte
}
var t T
// t == T{0, "", nil}
A value that is not of pointer, channel, map, or slice type cannot be nil.
Andrew
A pointer a memory address of a piece of data. Here is a video that
explains it all:
http://www.youtube.com/watch?v=UvoHwFvAvQE
But in short:
var i int = 1
var p *int = &i
i is a variable of type int with value 1
p is a variable of type pointer-to-int with value [memory address of i]
& means "address of" - it returns the memory address of a value.
* has two meanings, either "pointer to" (in a type definition) or
"dereference" (in an expression).
Dereferencing means "return the value at". Continuing the example above:
var j int = *p
j is a variable of type int with the value at the memory address p. In
this case, 1.
nil is the zero-value of a pointer type. (it doesn't point to anything)
Andrew
--
Regards,
-Clark
My guess is that...
func GetSiteUserByUsername(user string) *SiteUser {
conn, _ := mongo.Connect("127.0.0.1")
coll := conn.GetDB("mysite_dev").GetCollection("siteuser")
// Query the db.
qFindDoc, _ := mongo.Marshal(&SiteUser{Username:user})
bsonDocOut, _ := coll.FindOne(qFindDoc)
var foundUser *SiteUser
mongo.Unmarshal(bsonDocOut.Bytes(), &foundUser)
return foundUser
}
Needs to be...
func GetSiteUserByUsername(user string) *SiteUser {
conn, _ := mongo.Connect("127.0.0.1")
coll := conn.GetDB("mysite_dev").GetCollection("siteuser")
// Query the db.
qFindDoc, _ := mongo.Marshal(&SiteUser{Username:user})
bsonDocOut, _ := coll.FindOne(qFindDoc)
var foundUser SiteUser
mongo.Unmarshal(bsonDocOut.Bytes(), &foundUser)
return &foundUser
}
--
Regards,
-Clark
Well, you see, you can't actually DEREFERENCE the nil pointer. It
points to, literally, nothing. The object at the other end will be
garbage collected and any attempts to dereference will (as you saw) blow up.
It's all terminology.
On 08/03/10 21:32, ✖ chris tighe ✖ wrote:
--
Regards,
-Clark
Oh how I miss the days of segfaults.
--
Regards,
-Clark
var someObject *Object // Initialized to nil
someObject.SomeMethod()
--
Regards,
-Clark
In C it makes a lot of sense,
int *age; says, if you deference age you'll get a int
this is commonly miswritten as
int* age; with int* being referred to a type of 'pointer to int'
The Go syntax plays on this misunderstanding and changing it would be
be confusing to past C and C++ users without reason.
var age *int
- jessta
--
=====================
http://jessta.id.au
Rob wrote a good blog post on this topic:
http://blog.golang.org/2010/07/gos-declaration-syntax.html
Andrew
actually that's a common misunderstanding.
calling a method on a nil pointer does not necessarily
dereference the pointer. because the pointer has a known
type, the runtime knows which method to call without
looking inside the pointer. the method will get be called with a
receiver value.
one exception is when a value method is called on
a pointer value, in which case the runtime needs to
dereference the value in order to pass it to the method.
interfaces are not pointers, even though they can
be nil - calling a method on a nil interface
*will* panic.
http://www.cs.stanford.edu/cslibrary/PointerFunCBig.avi
(If you can ignore the fact that clay is talking to you,
it does do a good job of explaining what pointers are.)
Russ