Running server from shared library

Trying to run a servr from a shared library, so I did the below:

  1. Writting the shahred library as:
// server.go
package main

import (
import "C"

func hello(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "hello\n")
func headers(w http.ResponseWriter, req *http.Request) {
	for name, headers := range req.Header {
		for _, h := range headers {
			fmt.Fprintf(w, "%v: %v\n", name, h)
func main() {}

// export Run server
func Run(port string) {
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/headers", headers)
	if err := http.ListenAndServe("localhost:"+port, nil); err == nil {
		fmt.Println("listening to 8090")
	} else {
		fmt.Println("ListenAndServe: ", err)
  1. Compiling the shared library as:
$ go build -buildmode c-shared -o server.go
  1. Writting the main file that is calling the Run function in the shared library as:
package main

#cgo LDFLAGS: -ldl
#include <dlfcn.h>

void Run(char* port){}

import "C"

func main() {
	// handle := C.dlopen(C.CString(""), C.RTLD_LAZY)
	// C.dlsym(handle, C.CString("8090"))

	C.dlopen(C.CString(""), C.RTLD_LAZY)"8090"))
  1. Running the main file as:
$ go run main.go

The main function had been terminated directly, and the srver had not been run at http://localhost:8090/hello

You seem to be running on non-Windows, so I recommend looking into the plugin package.