Skip to content
go2hxPublic

About

Import Go libraries in your Haxe projects Go -> Haxe source-to-source compiler

Topics

Resources

Contributing

Stars

156 stars

Watchers

9 watching

Forks

Repository files navigation

build Static Badge Static Badge Discord

demo

Import Go libraries into your Haxe projects.

A spiritual successor to Elliott Stoneham's compiler

This project is not currently being worked on, we have changed focus to working on a Go target for Haxe. We took much of what we learned here to write it, along with the automatic extern generator it uses!

Tables of contents

Why

The Go programming language fills in the gap for some of Haxe's biggest weaknesses, that being:

  • Small ecosystem
  • Lack of documentation
  • Lack of cross target libraries

Go can be seen as an alternative to the Nodejs ecosystem:

traits go2hx nodejs
No externs needed ️️✅ ❌
Cross target ✅ ❌
Large ecosystem ✅ ✅
Thread support ✅ ❌
Production ready ❌ ✅
Has tests on the Haxe side ✅ ❌
Can create a web server ✅ ✅
Underlying code statically typed ️️✅ ❌

Why for Go devs

Makes Go code even more portable using go2hx to be able to compile to Javascript, Jvm, C++ and more in the future.

  • Allows Go to be able to be used as a full stack programming language, both backend and frontend.
  • Makes Go the perfect language to develop library drivers for multiple languages, only writing once in Go.

Language support

The compiler supports a sub-specification of the Go language, written about in detail here.

The gist is that the language is compatible with Go's specification except when using cgo, runtime/reflect internals, unsafe, or syscall.

There are many edge cases that limit the compiler from being able to produce correct library code. If you are wondering does "library/feature X work yet", the best way is to try the compiler out yourself.

Compatible Haxe Targets

Supported: Hashlink.

Partially supported: Eval

Planned: C++, Jvm, Javascript.

Javascript can only be supported with a coroutine model.

Installation

  1. Download Haxe 5 nightly version
  2. Git needs to be installed
  3. Hxcpp haxe library is required for the compiler
haxelib git hxcpp https://github.com/haxefoundation/hxcpp
  1. A C++ compiler is required to use Hxcpp, for more information.

  2. Install the compiler

haxelib git go2hx https://github.com/go2hx/go2hx

Usage

To compile a given library run the following command in the directory of your project:

haxelib run go2hx [library_here]

This will by default create a golibs folder and go2hx.*.zip HXB files * representing the supported Haxe targets.

Now include the outputted code into your build hxml or equivalent (needs to be the same location, where the compiler was invoked):

--hxb-lib go2hx.zip
-w -WStaticInitOrder # temporarily required to hide a Haxe compiler warning

Web server example

Requires

  1. go2hx installed
  2. hashlink downloaded

For example, to compile net/http to build a web server, run the command in a new project folder:

haxelib run go2hx net/http

Create a build.hxml file, in the same directory:

-m Main
--hxb-lib go2hx.zip
-w -WStaticInitOrder # temporarily required to hide a Haxe compiler warning
--hl test.hl

Create a Main.hx file:

import go.net.http.Http;
function main() {
    Http.handleFunc("/hello", (response:ResponseWriter, request:Request) -> {
        response.write("HELLO THERE");
    });
    Sys.println("starting!");
    Http.listenAndServe("127.0.0.1:8090", null);
}

Run the resulting code:

haxe build.hxml
hl test.hl

After starting! is shown navigate to the webserver: http://localhost:8090/hello

Voila! A working all in Haxe webserver. The go2hx compiler only needs to be run again to add/update Go packages.

For more information check the documentation!

Demo HTTP launch video on Youtube

IMAGE ALT TEXT HERE

Demo source code

import go.net.http.Http;

function main() {
    // "/hello" handle
    Http.handleFunc("/hello", (response, request) -> {
        // response.write("hello!");
        // Let's redirect instead
        Http.redirect(response, request, "/headers", statusFound);
    });
    // "/headers" handle
    Http.handleFunc("/headers", (response, request) -> {
        for (key => values in request.header) {
            response.write(key + "\n");
            for (value in values) {
                response.write("   " + value + "\n");
            }
        }
    });
    // turn Dir into an interface, Dir -> FileSystem
    final fileHandler = go.Go.asInterface(("." : Dir));
    // "/" handle
    Http.handle("/", Http.fileServer(fileHandler));
    // starting web server
    trace("starting!");
    Http.listenAndServe(":3000", null);
}

Setup from source

Clone the repo using the recursive flag:

git clone --recursive https://github.com/go2hx/go2hx
cd go2hx
haxelib dev go2hx .
haxelib run go2hx build

Community

Haxe #go2hx discord channel. Read the contributing guide for insights and information on adding code into the project.

Credits

A spiritual successor to Tardisgo written by Elliott Stoneham, A maintainer, mentor, and code contributor of this project, go2hx would not exist without him.

About

Import Go libraries in your Haxe projects Go -> Haxe source-to-source compiler

Topics

Resources

Contributing

Stars

156 stars

Watchers

9 watching

Forks

Releases

Contributors

Languages