github.com/notti/go-dynamic@v0.0.0-20190619201224-fc443047424c/example/main.go (about)

     1  // Smoke test/example demonstrating accessing global variables and functions from a self compiled library.
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"runtime"
     9  
    10  	"github.com/notti/nocgo"
    11  )
    12  
    13  func main() {
    14  	fmt.Println(os.Args) // check if startup works
    15  
    16  	var lib string
    17  	switch runtime.GOARCH {
    18  	case "386":
    19  		lib = "libcalltest32.so.1"
    20  	case "amd64":
    21  		lib = "libcalltest64.so.1"
    22  	default:
    23  		log.Fatalln("Unknown arch ", runtime.GOARCH)
    24  	}
    25  
    26  	l, err := nocgo.Open(lib)
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  
    31  	var testCall func(
    32  		i1 uint16,
    33  		i2 int32,
    34  		f1 float32,
    35  		f2 float64,
    36  		i3 int32,
    37  		i4 int32,
    38  		i5 int32,
    39  		i6 int32,
    40  		i7 int32,
    41  		i8 int32,
    42  		i9 int16,
    43  	) int32
    44  
    45  	var printCall func()
    46  
    47  	var testvalue *int
    48  
    49  	if err := l.Func("test_call", &testCall); err != nil {
    50  		log.Fatal(err)
    51  	}
    52  
    53  	if err := l.Func("print_value", &printCall); err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	fmt.Println(testCall(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11))
    58  
    59  	printCall()
    60  	err = l.Value("value", &testvalue)
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  	*testvalue = 100
    65  	printCall()
    66  }