package main
/*
#include <stdlib.h> // Include the C standard library for memory management functions
*/
import "C" // Import the C language features for interoperability
import "unsafe" // Import unsafe for pointer manipulation
// Fibonacci computes the first n Fibonacci numbers and returns a pointer to an array of integers.
// It takes an integer n (C.int) as an argument, which represents how many Fibonacci numbers to compute.
//
//export Fibonacci
func Fibonacci(n C.int) *C.int {
// Allocate memory for the result array in C
// Using C.malloc to allocate memory for an array of integers (size of n).
result := C.malloc(C.size_t(n) * C.size_t(unsafe.Sizeof(C.int(0))))
// Typecast the result pointer to a Go slice to populate it.
// The slice will allow us to easily access and modify the allocated memory.
resultSlice := (*[1 << 30]C.int)(result)[:n:n] // Create a slice of C.int from the allocated memory
// Initialize the first two Fibonacci numbers
if n > 0 {
resultSlice[0] = 0 // First Fibonacci number
}
if n > 1 {
resultSlice[1] = 1 // Second Fibonacci number
}
// Compute the Fibonacci sequence from the third number onward
for i := 2; i < int(n); i++ {
resultSlice[i] = resultSlice[i-1] + resultSlice[i-2] // F(n) = F(n-1) + F(n-2)
}
// Print a message indicating that the Go code is running
print("You're running the Go code\\n")
// Return a pointer to the allocated C array containing Fibonacci numbers
return (*C.int)(result)
}
// The main function is not needed for the library
// It is left empty to comply with the requirements of cgo, which needs a main function
// if the file is compiled into an executable, but here it will be built as a shared library.
func main() {}
go mod init goWrapper
go build -o libfibonacci.so -buildmode=c-shared .
go
: This is the Go programming language command-line tool. It's used to compile, build, and manage Go packages and modules.
build
: This subcommand tells the Go tool to compile the specified source files into an executable or library. In this case, we’re creating a shared library.
o libfibonacci.so
:
o
: This flag specifies the name of the output file.libfibonacci.so
: This is the name of the output file that will be created. The .so
extension indicates that this is a shared object file (a shared library) suitable for linking with other programs.buildmode=c-shared
:
buildmode
: This flag specifies the build mode for the Go compiler. The build mode defines how the output will be generated.c-shared
: This mode tells the Go compiler to create a shared library that can be linked with C programs (and by extension, other languages that can interact with C, such as Swift). It generates a shared object that can export Go functions for use in other languages.fibonacci.go
: This is the name of the Go source file that contains the code you want to compile. In this case, it’s the file where your Fibonacci function is defined.
or
.
: This dot represents the current directory. It indicates that the Go compiler should look for Go source files in the current directory to compile into the shared library. It allows you to compile all Go files in that directory without specifying each file individually.