There is a project on github that shows how to create a DLL based on and thanks to user 7155193.
You mainly use GCC to create DLLs from golang, the generated .a and .h files.
First, you create a simple Go file that exports a function (or more).
package main import "C" import "fmt" //export PrintBye func PrintBye() { fmt.Println("From DLL: Bye!") } func main() { // Need a main function to make CGO compile package as C shared library }
Compile it with
go build -buildmode=c-archive exportgo.go
Then you create the C program (goDLL.c), which will be referenced in the .h and .a files generated above
#include <stdio.h> #include "exportgo.h" // force gcc to link in go runtime (may be a better solution than this) void dummy() { PrintBye(); } int main() { }
Compile / Link DLL with GCC:
gcc -shared -pthread -o goDLL.dll goDLL.c exportgo.a -lWinMM -lntdll -lWS2_32
Then the goDLL.dll file can be downloaded to another C program, the freepascal / lazarus program, or your program of choice.
The full code with the lazarus / fpc project that loads the DLL is here: https://github.com/z505/goDLL
source share