Using Go Dynamic and Static Shared libraries in C/C++ Programs

In this post, I build a shared library and dynamic library by a Go source code and use it for spreadsheet decryption. A Shared Object/Library is a binary file with a dynamic/static loading table for functions.

Go language Source Code

// decrypt.go
package main

import "C"
import (


//export  decryption
func decryption(file *C.char, passwd *C.char) {
    f, err := excelize.OpenFile(C.GoString(file), excelize.Options{Password: C.GoString(passwd)})
    if err != nil {
    defer func() {
        if err := f.Close(); err != nil {
    if err := f.SaveAs("decrypted workbook.xlsx", excelize.Options{}); err != nil {

func main() {

Build shared libraries

Build a dynamic shared library

go build -o -buildmode=c-shared decrypt.go

It will generate two files: libdecrypt.a and libdecrypt.h.

Build a static shared library

go build -o libdecrypt.a -buildmode=c-archive

It will generate two files: and libdecrypt.h.

Using a dynamic shared library in C++ program

// decrypt.cpp
#include <stdio.h>
#include <unistd.h>

#include "libdecrypt.h"

int main() {
    decryption((char*)"encryptAES.xlsx", (char*)"password");

Build a executable file:

gcc -v decrypt.cpp -o decrypt ./

By executing the following command we can get decrypted spreadsheet: decrypted workbook.xlsx.


Using a static shared library in C program

// decrypt.c
#include "libdecrypt.h"

int main ()
    decryption("encryptAES.xlsx", "password");

Build a executable file:

gcc -o decrypt decrypt.c libdecrypt.a -lpthread

By executing the following command we can get decrypted spreadsheet.


That explains how to build the dynamic and static shared library (shared object) in Go, and how to use them.

5.00 avg. rating (99% score) - 6 votes