Skip to content

Software License Dongle for Go (Golang)

Go binds over cgo to the same core library. If shipping a single self-contained binary is the reason you chose Go, link the static library and you still ship one file — the dongle support goes inside it.

Go module version badge

go get github.com/AB-KeyNub/KeyNub-SDK/bindings/goCode language: Bash (bash)
ctx, err := keynub.NewContext()
if err != nil { return err }
defer ctx.Close()

dongle, err := ctx.Open("")          // first dongle found
if err != nil { return err }
defer dongle.Close()

if _, err := dongle.VerifyGenuine(); err != nil { return err }

session, err := dongle.OpenSession()
if err != nil { return err }
defer session.Close()

license, err := session.ReadRecord("license")Code language: Go (go)

A note on cgo

cgo is the trade-off here: it means a C toolchain on your build machines and it rules out CGO_ENABLED=0 builds. There is no way around that for a library that talks to USB hardware through the operating system’s HID stack, and it is the same trade-off any hardware-backed licensing makes in Go.

And the rule that matters in every language: do not branch on a boolean. Encrypt data your program genuinely needs with appEncrypt at build time and decrypt it through the dongle at run time, so removing the check leaves the program with nothing to compute rather than a working unlicensed copy.

Building and linking

cgo finds the headers by itself, but the directory holding the built native library is not knowable at compile time, so you pass it in through CGO_LDFLAGS. At run time the shared library has to be findable the usual way: PATH on Windows, the loader search path on Linux and macOS. Link the static library instead and the dongle support goes inside your binary, which is usually the point of having chosen Go.

Go 1.17 or later is required, because handing a Go progress callback down to C uses runtime/cgo.Handle rather than passing a Go pointer into C and hoping the garbage collector agrees.

Code

Runnable sample: go/verify_and_read. Binding source: bindings/go. Both are Apache-2.0, in the public SDK repository; the prebuilt native libraries are in the repository’s natives/ folder, one per platform.

All supported languages · All industries · Buy a KeyNub · Ask us something