本文整理汇总了Golang中github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub.RangeQueryState方法的典型用法代码示例。如果您正苦于以下问题:Golang ChaincodeStub.RangeQueryState方法的具体用法?Golang ChaincodeStub.RangeQueryState怎么用?Golang ChaincodeStub.RangeQueryState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub
的用法示例。
在下文中一共展示了ChaincodeStub.RangeQueryState方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Query
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
switch function {
case "get":
if len(args) < 1 {
return nil, errors.New("get operation must include one argument, a key")
}
key := args[0]
value, err := stub.GetState(key)
if err != nil {
return nil, fmt.Errorf("get operation failed. Error accessing state: %s", err)
}
return value, nil
case "keys":
keysIter, err := stub.RangeQueryState("", "")
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
}
defer keysIter.Close()
var keys []string
for keysIter.HasNext() {
key, _, err := keysIter.Next()
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
}
keys = append(keys, key)
}
jsonKeys, err := json.Marshal(keys)
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error marshaling JSON: %s", err)
}
return jsonKeys, nil
default:
return nil, errors.New("Unsupported operation")
}
}