Golang and Hyperledger Fabric ChainCode Development with VS Code — Quick Step-by-Step Guide

Juarez Junior
6 min readAug 4, 2018

by Juarez Junior

Introduction

As part of my current role, I work with the Oracle EMEA Blockchain Community as an Oracle Blockchain Cloud Service Champion.

Considering that Blockchain is currently a hot and emerging technology as we know, almost every day people ask me about how they can start their learning journey and then be able to develop Blockchain applications based on Hyperledger Fabric (HLF) and the Oracle Autonomous Blockchain Cloud Service, so I’ve decided to create this quick guide.

There’s a vast choice of programming languages that you can use for HLF development including Go, Java, and Javascript (Node.js). Nevertheless, as HLF (and Docker) do use Go as their base language, I decided to start with an explanation of how to develop HLF Chaincode (blockchain smart contracts) with the Go language.

So this is a quick step-by-step guide to help you configure your Go language (golang.org) development environment as well as compile your first Hyperledger Fabric Chaincode smart contract.

That’s your quick introduction to Blockchain application development with HLF — Hyperledger Fabric!

Below are the quick steps:

Download and install Go (golang)

Download the binaries for your target platform (Windows, macOS, Linux) and follow the installation instructions.

Configure the Go Env Variables — GOPATH, GOBIN, GOROOT (example for Windows below):

Golang environment variables example

As usual, the GOPATH directory should have the following sub-directories:

GOPATH and its sub-directory structure

Download and Install the VS Code IDE

You can use Eclipse (along with the goclipse plugin) as your tool for Go development.

However, I like Visual Studio Code most as it’s a polyglot IDE (Integrated Development Environment).

Visual Studio Code is a lightweight yet powerful source code editor which runs on your desktop and is available for Windows, macOS, and Linux. It comes with built-in support for JavaScript, TypeScript, and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, and Go).

There’s an official Go extension for Visual Studio Code developed by Microsoft that’s really nice. You can learn more about all its features here.

Launch VS Code and install the golang extension

No tricks here, just press Ctrl + Shift + X, then type golang and select the plugin shown below.

Golang extension for VS Code

Open the project under %GOPATH%/src where your Go source code for the HLF Chaincode smart contract is located

Select File -> Open Folder (or press Ctrl + K Ctrl + O) and select the root folder, the project folder is where the Go source code for our smart contract is located. On my Windows environment the golang source file for our sample smart contract is located in the directory C:\golang-projects\src\petTrace\petTrace.go.

peTrace smart contract directory

It will be loaded and opened then, as shown below:

petTrace project and HLF chaincode (smart contract)

You can get the petTrace.go sample chaincode (smart contract) below:

Open the petTrace.go file (double-click on it).

petTrace.go source code

In case you have the dependencies already in place, there will be no errors at all and you can skip step number 6 below. Otherwise, please check step number 6 below, fix the dependencies issues, and proceed to step 7.

Confirm that your Hyperledger dependencies are in place

In my environment, I have cloned both Hyperledger 1.1 and Hyperledger 1.0 and I have different folders for them under C:\golang-projects\src\github.com as shown below:

For the simplistic smart contract we’re using here (petTrace.go), the HLF version is not so critical because it does not use any specific interface contract that could have changed from HL 1.0 to 1.1.

You can get more information about Hyperledger 1.1, upgrades, and a summary of differences here.

When we deploy our chaincode to Oracle Autonomous Blockchain Cloud Service (the underlying framework is HLF as you may know) and then request the chaincode instantiation, we provide only the source code for the smart contract and not the binaries, so it will be compiled internally anyway using the right version, the current one as provided by OABCS.

The ZIP file only contains the directory and the source code (petTrace.go) so the actual compilation is performed internally in OABCS and not locally in your development environment. For this simple example then it would also be possible to compile it locally using Hyperledger 1.1 in order to make sure that your code can compile as well as that your code is somehow correct as per the HLF Shim’s interface contract.

Compile the chaincode locally on VS Code

In order to compile locally and make sure there are no errors (syntax, API usage, etc), select the terminal tab (Windows Powershell).

VS Code terminal tab

Then type go build petTrace.go and then hit ENTER.

go build petTrace.go

The compilation will occur and provided that you have no errors, a binary file (for example, petTrace.exe for Windows) will be generated (highlighted in red on the screenshot below).

Successful chaincode compilation and executable output file -> petTrace.exe

Provided that everything is OK you can create your ZIP file and instantiate it on Oracle Autonomous Blockchain Cloud Service / HLF as usual.

The next blog post about our Oracle Autonomous Blockchain Cloud Service will explain how you can create a blockchain network and deploy your Chaincode (smart contract).

Otherwise, in case your code has issues, you will see the error messages as well as error highlights on the VS Code editor.

I can simulate one error here just by trying to import a package that does not exist, juarez for example.

The source code file name as well as the code editor will highlight the issues in red as shown below.

Simulated error / bug / typing issue

At last, I can try to compile the code with the error (the wrong package name) and I will get the messages below:

Build error

Cheat Sheets — Keyboard shortcuts for Windows / Mac — Visual Studio Code

As cited before, I like VS Code because it is a lightweight and polyglot IDE. One last tip is that there are good Cheat Sheets for VS Code available.

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf

Wrap up

That’s it and I hope that helps you regarding the use of VS Code for Golang and Chaincode development.

Happy Hyperledger chaincode development, I hope that this quick step-by-step guide and the explanations can help you innovate happily!

Have fun building great things with the Oracle Blockchain Cloud Service!

Oracle Developers and Oracle OCI Free Tier

Join our Oracle Developers channel on Slack to discuss Microservices, Java, JDBC, Oracle Cloud Infrastructure, Oracle Database, and other topics!

Build, test, and deploy your applications on Oracle Cloud — for free! Get access to OCI Cloud Free Tier!

--

--