addrbook.go
func (a *AddrBook) loadFromFile(filePath string) bool {
	// If doesn't exist, do nothing.
	_, err := os.Stat(filePath)
	if os.IsNotExist(err) {
		return false
	}

	// Load addrBookJSON{}
	r, err := os.Open(filePath)
	if err != nil {
		PanicCrisis(Fmt("Error opening file %s: %v", filePath, err))
	}
	defer r.Close()
	aJSON := &addrBookJSON{}
	dec := json.NewDecoder(r)
	err = dec.Decode(aJSON)
	if err != nil {
		PanicCrisis(Fmt("Error reading file %s: %v", filePath, err))
	}

	// Restore all the fields...
	// Restore the key
	a.key = aJSON.Key
	// Restore .addrNew & .addrOld
	for _, ka := range aJSON.Addrs {
		for _, bucketIndex := range ka.Buckets {
			bucket := a.getBucket(ka.BucketType, bucketIndex)
			bucket[ka.Addr.String()] = ka
		}
		a.addrLookup[ka.Addr.String()] = ka
		if ka.BucketType == bucketTypeNew {
			a.nNew++
		} else {
			a.nOld++
		}
	}
	return true
}
func (a *AddrBook) loadFromFile(filePath string) bool {
	// If doesn't exist, do nothing.
	_, err := os.Stat(filePath)
	if os.IsNotExist(err) {
		return false
	}

	// Load addrBookJSON{}
	r, err := os.Open(filePath)
	if err != nil {
		PanicCrisis(Fmt("Error opening file %s: %v", filePath, err))
	}
	defer r.Close()
	aJSON := &addrBookJSON{}
	dec := json.NewDecoder(r)
	err = dec.Decode(aJSON)
	if err != nil {
		PanicCrisis(Fmt("Error reading file %s: %v", filePath, err))
	}

	// Restore all the fields...
	// Restore the key
	a.key = aJSON.Key
	// Restore .addrNew & .addrOld
	for _, ka := range aJSON.Addrs {
		for _, bucketIndex := range ka.Buckets {
			bucket := a.getBucket(ka.BucketType, bucketIndex)
			bucket[ka.Addr.String()] = ka
		}
		a.addrLookup[ka.Addr.String()] = ka
		if ka.BucketType == bucketTypeNew {
			a.nNew++
		} else {
			a.nOld++
		}
	}
	return true
}
func ( a * AddrBook ) loadFromFile ( filePath string ) bool { // If doesn't exist, do nothing. _ , err := os . Stat ( filePath ) if os . IsNotExist ( err ) { return false } // Load addrBookJSON{} r , err := os . Open ( filePath ) if err != nil { PanicCrisis ( Fmt ( " " , filePath , err ) ) } defer r . Close ( ) aJSON := & addrBookJSON { } dec := json . NewDecoder ( r ) err = dec . Decode ( aJSON ) if err != nil { PanicCrisis ( Fmt ( " " , filePath , err ) ) } // Restore all the fields... // Restore the key a . key = aJSON . Key // Restore .addrNew & .addrOld for _ , ka := range aJSON . Addrs { for _ , bucketIndex := range ka . Buckets { bucket := a . getBucket ( ka . BucketType , bucketIndex ) bucket [ ka . Addr . String ( ) ] = ka } a . addrLookup [ ka . Addr . String ( ) ] = ka if ka . BucketType == bucketTypeNew { a . nNew ++ } else { a . nOld ++ } } return true }
// Returns false if file does not exist.
// Panics if file is corrupt.
Returns false if file does not exist . Panics if file is corrupt .