Godeps/_workspace/src/gopkg.in/mgo.v2/session.go
func (iter *Iter) Next(result interface{}) bool {
	iter.m.Lock()
	iter.timedout = false
	timeout := time.Time{}
	for iter.err == nil && iter.docData.Len() == 0 && (iter.docsToReceive > 0 || iter.op.cursorId != 0) {
		if iter.docsToReceive == 0 {
			if iter.timeout >= 0 {
				if timeout.IsZero() {
					timeout = time.Now().Add(iter.timeout)
				}
				if time.Now().After(timeout) {
					iter.timedout = true
					iter.m.Unlock()
					return false
				}
			}
			iter.getMore()
			if iter.err != nil {
				break
			}
		}
		iter.gotReply.Wait()
	}

	// Exhaust available data before reporting any errors.
	if docData, ok := iter.docData.Pop().([]byte); ok {
		close := false
		if iter.limit > 0 {
			iter.limit--
			if iter.limit == 0 {
				if iter.docData.Len() > 0 {
					iter.m.Unlock()
					panic(fmt.Errorf("data remains after limit exhausted: %d", iter.docData.Len()))
				}
				iter.err = ErrNotFound
				close = true
			}
		}
		if iter.op.cursorId != 0 && iter.err == nil {
			iter.docsBeforeMore--
			if iter.docsBeforeMore == -1 {
				iter.getMore()
			}
		}
		iter.m.Unlock()

		if close {
			iter.Close()
		}
		err := bson.Unmarshal(docData, result)
		if err != nil {
			debugf("Iter %p document unmarshaling failed: %#v", iter, err)
			iter.m.Lock()
			if iter.err == nil {
				iter.err = err
			}
			iter.m.Unlock()
			return false
		}
		debugf("Iter %p document unmarshaled: %#v", iter, result)
		// XXX Only have to check first document for a query error?
		err = checkQueryError(iter.op.collection, docData)
		if err != nil {
			iter.m.Lock()
			if iter.err == nil {
				iter.err = err
			}
			iter.m.Unlock()
			return false
		}
		return true
	} else if iter.err != nil {
		debugf("Iter %p returning false: %s", iter, iter.err)
		iter.m.Unlock()
		return false
	} else if iter.op.cursorId == 0 {
		iter.err = ErrNotFound
		debugf("Iter %p exhausted with cursor=0", iter)
		iter.m.Unlock()
		return false
	}

	panic("unreachable")
}
func (iter *Iter) Next(result interface{}) bool {
	iter.m.Lock()
	iter.timedout = false
	timeout := time.Time{}
	for iter.err == nil && iter.docData.Len() == 0 && (iter.docsToReceive > 0 || iter.op.cursorId != 0) {
		if iter.docsToReceive == 0 {
			if iter.timeout >= 0 {
				if timeout.IsZero() {
					timeout = time.Now().Add(iter.timeout)
				}
				if time.Now().After(timeout) {
					iter.timedout = true
					iter.m.Unlock()
					return false
				}
			}
			iter.getMore()
			if iter.err != nil {
				break
			}
		}
		iter.gotReply.Wait()
	}

	// Exhaust available data before reporting any errors.
	if docData, ok := iter.docData.Pop().([]byte); ok {
		close := false
		if iter.limit > 0 {
			iter.limit--
			if iter.limit == 0 {
				if iter.docData.Len() > 0 {
					iter.m.Unlock()
					panic(fmt.Errorf("data remains after limit exhausted: %d", iter.docData.Len()))
				}
				iter.err = ErrNotFound
				close = true
			}
		}
		if iter.op.cursorId != 0 && iter.err == nil {
			iter.docsBeforeMore--
			if iter.docsBeforeMore == -1 {
				iter.getMore()
			}
		}
		iter.m.Unlock()

		if close {
			iter.Close()
		}
		err := bson.Unmarshal(docData, result)
		if err != nil {
			debugf("Iter %p document unmarshaling failed: %#v", iter, err)
			iter.m.Lock()
			if iter.err == nil {
				iter.err = err
			}
			iter.m.Unlock()
			return false
		}
		debugf("Iter %p document unmarshaled: %#v", iter, result)
		// XXX Only have to check first document for a query error?
		err = checkQueryError(iter.op.collection, docData)
		if err != nil {
			iter.m.Lock()
			if iter.err == nil {
				iter.err = err
			}
			iter.m.Unlock()
			return false
		}
		return true
	} else if iter.err != nil {
		debugf("Iter %p returning false: %s", iter, iter.err)
		iter.m.Unlock()
		return false
	} else if iter.op.cursorId == 0 {
		iter.err = ErrNotFound
		debugf("Iter %p exhausted with cursor=0", iter)
		iter.m.Unlock()
		return false
	}

	panic("unreachable")
}
func ( iter * Iter ) Next ( result interface { } ) bool { iter . m . Lock ( ) iter . timedout = false timeout := time . Time { } for iter . err == nil && iter . docData . Len ( ) == 0 && ( iter . docsToReceive > 0 || iter . op . cursorId != 0 ) { if iter . docsToReceive == 0 { if iter . timeout >= 0 { if timeout . IsZero ( ) { timeout = time . Now ( ) . Add ( iter . timeout ) } if time . Now ( ) . After ( timeout ) { iter . timedout = true iter . m . Unlock ( ) return false } } iter . getMore ( ) if iter . err != nil { break } } iter . gotReply . Wait ( ) } // Exhaust available data before reporting any errors. if docData , ok := iter . docData . Pop ( ) . ( [ ] byte ) ; ok { close := false if iter . limit > 0 { iter . limit -- if iter . limit == 0 { if iter . docData . Len ( ) > 0 { iter . m . Unlock ( ) panic ( fmt . Errorf ( " " , iter . docData . Len ( ) ) ) } iter . err = ErrNotFound close = true } } if iter . op . cursorId != 0 && iter . err == nil { iter . docsBeforeMore -- if iter . docsBeforeMore == - 1 { iter . getMore ( ) } } iter . m . Unlock ( ) if close { iter . Close ( ) } err := bson . Unmarshal ( docData , result ) if err != nil { debugf ( " " , iter , err ) iter . m . Lock ( ) if iter . err == nil { iter . err = err } iter . m . Unlock ( ) return false } debugf ( " " , iter , result ) // XXX Only have to check first document for a query error? err = checkQueryError ( iter . op . collection , docData ) if err != nil { iter . m . Lock ( ) if iter . err == nil { iter . err = err } iter . m . Unlock ( ) return false } return true } else if iter . err != nil { debugf ( " " , iter , iter . err ) iter . m . Unlock ( ) return false } else if iter . op . cursorId == 0 { iter . err = ErrNotFound debugf ( " " , iter ) iter . m . Unlock ( ) return false } panic ( " " ) }
// Next retrieves the next document from the result set, blocking if necessary.
// This method will also automatically retrieve another batch of documents from
// the server when the current one is exhausted, or before that in background
// if pre-fetching is enabled (see the Query.Prefetch and Session.SetPrefetch
// methods).
//
// Next returns true if a document was successfully unmarshalled onto result,
// and false at the end of the result set or if an error happened.
// When Next returns false, the Err method should be called to verify if
// there was an error during iteration.
//
// For example:
//
//    iter := collection.Find(nil).Iter()
//    for iter.Next(&result) {
//        fmt.Printf("Result: %v\n", result.Id)
//    }
//    if err := iter.Close(); err != nil {
//        return err
//    }
//
Next retrieves the next document from the result set blocking if necessary . This method will also automatically retrieve another batch of documents from the server when the current one is exhausted or before that in background if pre - fetching is enabled ( see the Query . Prefetch and Session . SetPrefetch methods ) . Next returns true if a document was successfully unmarshalled onto result and false at the end of the result set or if an error happened . When Next returns false the Err method should be called to verify if there was an error during iteration . For example : iter : = collection . Find ( nil ) . Iter () for iter . Next ( &result ) { fmt . Printf ( Result : %v \ n result . Id ) } if err : = iter . Close () ; err ! = nil { return err }