I think you have found a partial answer. You could use the function GetStateByRange? This function doesn't return keys, it returns an iterator with the values.
You could use the GetStateByRange function and pass the date range to it.
Please refer to the example below:
func get_all_components(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
fmt.Println("starting get_all_components")
// input sanitation
err = sanitize_arguments_len(args, 1)
if err != nil {
return shim.Error(err.Error())
}
// ---- Get All Components ---- //
resultsIterator, err := stub.GetStateByRange("c0", "c9999999999999999999")
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
//queryKeyAsStr, queryValAsBytes, err := resultsIterator.Next()
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("get_all_components:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}