About this post
Imagine you are writing an app in Go and found a library with all the APIs you need. You will like to integrate it into your app. However, it is not written in Go. What options are available to you?
The options for you depends on the kind of language that was the basis of your library. If it was written in C/C++ you could use cgo
. You could also use gRPC/protobuf
or if you search the internet, you might even find some custom solutions.
In this post, I am going to describe a way to do it via WebAssembly, a tool call wasmer-go and using Go file embedding (version 1.16 onwards) capabilities.
WebAssembly
Here is the official definition of the technology:
WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
I won’t go into the details of the inner workings of Wasm. Please refer to the official document. In the context of this post, we could think of Wasm as a byte code that can be executed by a Wasm runtime engine; not too dissimilar to Java Virtual Machine (JVM), up to a point.
The source code of a Wasm is raw text known as an assembly code. To illustrate a concept, we have a simple math
module, written in Wasm assembly to perform some arithmetic operations: 1) add the two integer values, 2) multiply the sum by 10, and 3) export a method name getAns
for a calling application to get results. Our Wasm aseembly is as follows:
For any practical applications, you will not likely be writing Wasm in assembly form. Chances are you would be compiling from a programming language like C/C++, Rust, Go and more. This is beyond the scope of this post and I suggest you refer to the Wasm official document for details.
The Wasm assembly as shown above will need to be compiled into byte codes. Let’s assumed that the assembly code is in a file name math.wat
. You will run the command, wat2wasm math.wat -o math.wasm
which will convert the assembly code into the following byte code found in the file math.wasm
:
Integrating into our Go app
Wasm byte code is typically executed in a browser based or Javascript runtime engine. An example of a Wasm embedded in NodeJS is as shown here:
To get Wasm byte code to run in Go, you can use this tool call wasmer-go
and it is found in this repository https://github.com/wasmerio/wasmer-go.
In our project for this post, our goal is to create a simple native app. Our implementation is as follows:
When you build this code – i.e. go build
– it will generate a executable with the math.wasm
file encoded.
Through this approach, you now have the ability to integrate library written in other languages.
Summing up
To sum it up, if you elect to integrate a library using other languages via Wasm into your Go app, the steps are:
STEP 1: Generate a Wasm assembly and/or byte code using the tools from the offical source or third parties tool.
STEP 2: Embed the *.wasm
or byte code file into your app (Go v1.16 onwards use the embed package or appropriate tool for earlier version).
STEP 3: Use the wasmer-go
module as the basis of your Wasm runtime.
STEP 4: Build your app as per the Go tools.
Having learnt this approach to integrating Go app with non-Go libraries, you may feel that Wasm is the way to go, pardon the pun. For that, I shall leave it to you to decide.
From my perspective, I see great potential for Wasm as a basis for a Smart Contract for multiple blockchain protocol but it is a topic I hope to share in future posts after my experimentation.