An open API service indexing awesome lists of open source software.

https://github.com/zeeshan2k2/go-wrapper-for-swift

Integrate Go code into an iOS app using Swift to compute Fibonacci numbers via a C shared library.
https://github.com/zeeshan2k2/go-wrapper-for-swift

c go shared-library swift wrapper

Last synced: 2 months ago
JSON representation

Integrate Go code into an iOS app using Swift to compute Fibonacci numbers via a C shared library.

Awesome Lists containing this project

README

        

Go-Swift Integration: Fibonacci Sequence Example

This repository demonstrates how to integrate Go code into an iOS project using Swift. The primary focus is showcasing the use of Go to compute the Fibonacci sequence and expose it to Swift through a shared library.

For a more in-depth look at the implementation details, including code examples and explanations, click on this link: Detailed Implementation.

Concept Overview


This project serves as an example of cross-language interaction between Go and Swift using cgo. The Go code calculates the Fibonacci sequence and is compiled into a shared C-compatible library (.so) that can be imported and utilized within a Swift iOS application.

Contents




  • Go Code: Contains Go code to compute the Fibonacci sequence. It uses cgo to export functions in a way that can be used in Swift.


  • Swift iOS Project: A basic iOS project that integrates the Go library. The Swift app calls the Go-compiled shared library to compute and display Fibonacci numbers.

Workflow




  • Go Code (fibonacci.go): The Go code uses cgo to expose a Fibonacci function, which allocates and returns the sequence as a C array.


  • Build Shared Library: The Go code is compiled into a shared object (libfibonacci.so) that can be linked with C-compatible languages like Swift.


  • Swift iOS Project: The iOS app dynamically links the shared Go library and uses it to compute Fibonacci numbers directly within the app.

How It Works


The Go code is made compatible with C via cgo, allowing it to be compiled as a shared library (.so). The Swift project then loads this shared library at runtime and calls the Go function as if it were a native Swift function. This allows us to leverage Go’s performance and logic in a Swift-based iOS application.

Getting Started


Compile the Go Code:


Use the Go build command to compile the shared library:


go build -o libfibonacci.so -buildmode=c-shared .

Integrate in Swift:


The Swift iOS app dynamically loads the compiled Go library using dlopen and calls the exposed Fibonacci function.