import ( "github.com.openblockchain.obc-peer.openchain.chaincode.shim" ) func (t *MyChaincode) MyFunction(stub shim.ChaincodeStubInterface) pb.Response { err := stub.PutState("key1", []byte("value1")) if err != nil { return shim.Error("Failed to store data in the blockchain") } return shim.Success(nil) }
import ( "encoding/json" "github.com.openblockchain.obc-peer.openchain.chaincode.shim" ) type MyStruct struct { Name string `json:"name"` Age int `json:"age"` } func (t *MyChaincode) MyFunction(stub shim.ChaincodeStubInterface) pb.Response { myStruct := MyStruct{Name: "John", Age: 30} myStructBytes, err := json.Marshal(myStruct) if err != nil { return shim.Error("Failed to convert struct to bytes") } err = stub.PutState("key2", myStructBytes) if err != nil { return shim.Error("Failed to store data in the blockchain") } return shim.Success(nil) }Both examples use the `PutState` method to store data in the blockchain. Example 1 stores a simple string value, while example 2 stores a more complex struct after converting it to bytes. By using the `ChaincodeStubInterface` and its `PutState` method, a developer can securely store and retrieve data on the blockchain.