dexecve 1.3.0 dexecve: ^1.3.0 copied to clipboard
Replace the running process, with a POSIX execve system call.
dexecve #
Replace the running process, with a POSIX execve system call.
Usage #
import 'package:dexecve/dexecve.dart';
void main() {
dexecve('ping', ['1.1.1.1']);
}
see ./example/main.dart for more details
Dart FFI & Golang #
This project is a working example of how to integrate dartlang and golang using dart's c interop.
Steps basically look like this:
-
Create a golang
c-shared
library.hello.go
package main import "C" import ( "fmt" ) //export HelloWorld func HelloWorld() { fmt.Println("hello world") } func main() {}
-
Compile the library. eg:
go build -o libhello.so -buildmode=c-shared hello.go
You may want to cross compile the library for multiple platforms. Cross compilation of a static golang binary is relatively straight forward these days however it's not as easy to cross compile a library as
cgo
is required which normally is disabled for cross compilation.I have found xgo very helpful for this.
-
Write the dart ffi code to interface with the library. This feels almost like writing TypeScript typings for a JavaScript library.
hello.dart
import 'dart:ffi'; DynamicLibrary _lib = DynamicLibrary.open('libhello.so'); typedef HelloWorld = void Function(); typedef HelloWorld_func = Void Function(); final HelloWorld helloWorld = _lib.lookup<NativeFunction<HelloWorld_func>>('HelloWorld').asFunction();
-
Consume the function in your dart code and profit :)
main.dart
import 'hello.dart'; void main() { helloWorld(); }
Essentially all this project does is call golang's
syscall.Exec()
function.
I understand this might be overkill and a much smaller package could be created if I used C directly. One day I might do just that...
In my opinion this creates a very powerful tool chain, dartlang feels very familiar with Classes, Generics, Extension Methods, Exceptions, Async/Await and many other concepts that other more main stream languages have had for a very long time.
While golang offers a very powerful concurrency model, handy tools like defer
,
a simplified programming model, native performance and a larger ecosystem which
can help to fill any gaps in the current dart ecosystem.
Anything I can do in Go and I can do in Dart!
Further Resources #
- https://dart.dev/guides/libraries/c-interop
- https://github.com/dart-lang/sdk/issues/37509
- https://medium.com/@kyorohiro/code-server-x-dart-fmi-x-go-x-clang-2a5bd440fad8
- https://github.com/vladimirvivien/go-cshared-examples
- https://medium.com/swlh/build-and-use-go-packages-as-c-libraries-889eb0c19838
- https://medium.com/@walkert/fun-building-shared-libraries-in-go-639500a6a669
- https://github.com/crazy-max/xgo