Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/achille-roussel/go-ffi
Go bindings to libffi
https://github.com/achille-roussel/go-ffi
Last synced: 15 days ago
JSON representation
Go bindings to libffi
- Host: GitHub
- URL: https://github.com/achille-roussel/go-ffi
- Owner: achille-roussel
- License: mit
- Created: 2016-01-07T17:47:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-15T05:13:04.000Z (almost 9 years ago)
- Last Synced: 2023-07-14T18:41:30.987Z (over 1 year ago)
- Language: Go
- Size: 58.6 KB
- Stars: 23
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-ffi [![Build Status](https://travis-ci.org/achille-roussel/go-ffi.svg)](https://travis-ci.org/achille-roussel/go-ffi) [![Coverage Status](https://coveralls.io/repos/achille-roussel/go-ffi/badge.svg?branch=master&service=github)](https://coveralls.io/github/achille-roussel/go-ffi?branch=master)
======Go bindings to libffi
Calling C Functions
-------------------go-ffi is interesting when a program needs to call C functions in a way that is
not yet supported by cgo. For example if the function pointer was obtained
dynamically or if the function as a variadic signature.Here's an example showing how to call printf from Go using the go-ffi package:
```go
package main// #include
//
// const void *printf__ = (void *) printf;
import "C"
import "github.com/achille-roussel/go-ffi"func main() {
ffi.Call(C.printf__, nil, "%s %s!\n", "Hello", "World")
}
```
```
Hello World!
```Calling Go Functions
--------------------go-ffi also provides a mechanism for generating pointers that can be used to
call Go functions from compiled C code.
Here's an example showing how this is done:
```go
package mainimport (
"fmt"
"strconv"
"unsafe""github.com/achille-roussel/go-ffi"
)func main() {
itoa := ffi.Closure(strconv.Itoa)
fptr := itoa.Pointer()
repr := ""ffi.Call(unsafe.Pointer(fptr), &repr, 42)
fmt.Println(repr)
}
```
```
42
```Type Conversions
----------------go-ffi automatically converts between C and Go types when using the high-level
interfaces.
The following table exposes what conversions are supported:| Go | C |
|----------------|-----------------|
| int | int |
| int8 | int8_t |
| int16 | int16_t |
| int32 | int32_t |
| int64 | int64_t |
| uint | unsigned int |
| uint8 | uint8_t |
| uint16 | uint16_t |
| uint32 | uint32_t |
| uintptr | size_t |
| float32 | float |
| float64 | double |
| string | char * |
| unsafe.Pointer | void * |