timshannon / bolthold

BoltHold is an embeddable NoSQL store for Go types built on BoltDB
MIT License
648 stars 46 forks source link

his error occurs when fetching data from a specific bucket #111

Closed hanjiangjiaolong closed 4 years ago

hanjiangjiaolong commented 4 years ago

his error occurs when fetching data from a specific bucket image image

timshannon commented 4 years ago

var bucket is only valid inside your Bolt().Update func.

https://godoc.org/github.com/etcd-io/bbolt#Tx.Bucket

You can't reference it outside of that transaction.

hanjiangjiaolong commented 4 years ago

but,i want get data,What i need to do

timshannon commented 4 years ago

You don't need to create the buckets manually.

Just insert your model:

store, err := bolthold.Open(filename, 0666, nil)
if err != nil {
    //handle error
}
err = store.Insert("king", &modeluserModel{
    UserName:    "king",
    Password: "2959802013_",
})

dwedw := &model.UserModel{}
_ = store.Get("king", dwedw)
fmt.Println(dwedw)
hanjiangjiaolong commented 4 years ago

When I have a default bucket, when I want to have a second bucket, should I innovate to create a store instance?

timshannon commented 4 years ago

I'm not exactly following what you mean, but if you want to manually create and manage buckets, then I wouldn't suggest using Bolthold. Bolthold creates and manages it's own buckets based on the name of the type.

hanjiangjiaolong commented 4 years ago

I have two types of data with different data structures, how to put them in the default bucket of Bolthold

timshannon commented 4 years ago

Bolthold automatically creates a bucket for each type you put in. Use a new type and you get a new bucket.

If you really need to store two different types in the same bucket you can use an anonymous struct and combine the two.

struct {
  Type1
  Type2
}
hanjiangjiaolong commented 4 years ago

This seems to be a good solution