storm
storm copied to clipboard
Question: Quick way to check if key exists?
Hello! Great wrapper, I accidenlty found it, I think "toolkit" a little bit misleading on go-awesome, but may be it's just me...
If there a short cut to check if a key exists? Without creating a struct and pointer there? I understand it may be not a big deal, but still?
Hello !
Great wrapper, I accidenlty found it, I think "toolkit" a little bit misleading on go-awesome, but may be it's just me...
Yes, thanks for your feedback, it's kinda hard to find an accurate definition
If there a short cut to check if a key exists? Without creating a struct and pointer there?
There is no builtin way to check if a key exists in Bolt or in Storm but there are a few tricks:
Using GetBytes
:
_, err := db.GetBytes("YourStruct", "TheKey")
if err == storm.ErrNotFound {
// doesn't exists
}
...
GetBytes
copies the data returned by Bolt so it can be accessible even after the transaction is closed. When testing if a key exists, this copy is unnecessary.
Using Bucket
, you can directly access the Bolt API:
v := db.Bucket("YourStruct").Get([]byte("YourKey"))
if v == nil {
// doesn't exists
}
...
...
Hello, Thanks a lot, cool trick I will proceed with this :-)