All Products
Search
Document Center

Blockchain as a Service:Use Fabric Go SDK

Last Updated:Apr 29, 2020

Set up Golang development environment

First, make sure Golang binary has been installed correctly. The official Go installation document. The Go versions >=1.10.1, <1.11 have been tested and are suggested to use. To verify Go is correctly installed, you can run the following commands:

  1. $ go version
  2. go version go1.10.1
  3. $ go env GOPATH
  4. /root/gos

Download sample codes

Please download go-sdk-demo-1.4.5. Download sample codes to local directory: $GOPATH/src, and decompress it. Copy file connection-profile.yaml to path $GOPATH/src/go-sdk-demo. The layout of the workspace should look like this:

  1. go-sdk-demo
  2. ├── chaincode/
  3. ├── main.go
  4. ├── play.sh
  5. ├── README.md
  6. └── vendor/
  7. └── connection-profile.yaml
  • play.sh: An entry script that provides an interactive command line interface.
  • main.go: An example program based on Hyperledger Fabric Go SDK.
  • chaincode/sacc.out: A packaged chain code, it can be uploaded directly to BaaS console.
  • chaincode/src: The source codes of sacc.out.
  • vendor/github.com/hyperledger/fabric-sdk-go: The source codes of Hyperledger Fabric Go SDK.

Upload the Chaincode

Upload the chaincode: chaincode/sacc.out to BaaS and instantiate it. You can refer to Deploy chaincodes.

Run the sample

  1. cd $GOPATH/src/go-sdk-demo
  2. bash play.sh

Follow the prompts to enter some necessary information. play.sh will run main.go, which is a sample program based on the Hyperledger Fabric Go SDK, mainly to help you understand the basic usage of Go SDK. It includes the following functions:

  1. Enroll a user, download certificates.
  2. Query channel config and print it in console.
  3. Query channel info and print it in console.
  4. Query chaincode.
  5. Invoke chaincode.
  6. Query chaincode again, to make sure invoking is the success.

To Get more information about Hyperledger Fabric Go SDK, please refer to: fabric-sdk-go.