Git Product home page Git Product logo

dexecve's Introduction

dexecve

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

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:

  1. Create a golang c-shared library.

    hello.go

    package main
    
    import "C"
    
    import (
    	"fmt"
    )
    
    //export HelloWorld
    func HelloWorld() {
    	fmt.Println("hello world")
    }
    
    func main() {}
  2. 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.

  3. 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();
  4. 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.