Pub Version .github/workflows/main.yml semantic-release Conventional Commits KeepAChangelog License

Replace the running process, with a POSIX execve system call.


import 'package:dexecve/dexecve.dart';

void main() {
  dexecve('ping', ['']);

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:

  1. Create a golang c-shared library.


    package main
    import "C"
    import (
    //export HelloWorld
    func HelloWorld() {
     fmt.Println("hello world")
    func main() {}
  2. Compile the library. eg: go build -o -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.

  3. Write the dart ffi code to interface with the library. This feels almost like writing TypeScript typings for a JavaScript library.


    import 'dart:ffi';
    DynamicLibrary _lib ='');
    typedef HelloWorld = void Function();
    typedef HelloWorld_func = Void Function();
    final HelloWorld helloWorld = _lib.lookup<NativeFunction<HelloWorld_func>>('HelloWorld').asFunction();
  4. Consume the function in your dart code and profit :)


    import 'hello.dart';
    void main() {

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