Integration with Go
The Go programming language (or simply Golang) has an easy integration with native shared libraries. It can be considered as the next generation of the C and C++ programming languages and it calls itself a system programming language. Therefore, we expect to load and use the native libraries easily when using Golang.
In Golang, we use a built-in package called cgo to call C code and load the shared object files. In the following Go code, you see how to use the cgo
package and use it to call the C functions loaded from the C stack library file. It also defines a new class, Stack
, which is used by other Go code to use the C stack functionalities:
package main /* #cgo CFLAGS: -I.. #cgo LDFLAGS: -L.. -lcstack #include "cstack.h" */ import "C" import ( "fmt" ) type Stack struct { handler *C.cstack_t } func NewStack() *Stack { s := new(Stack) s.handler = C.cstack_new() C.cstack_ctor(s.handler, 100) return s } func (s *Stack...