Import Go libraries into your Haxe projects.
Imported Go libraries can be used across all supported Haxe targets.
Think of go2hx as a replacement for nodejs + the npm ecosystem:
- No externs required
- Concurrent by default
- No reliance on javascript
A spiritual successor to Elliott Stoneham's compiler
- Why?
- Language support
- Installation
- Usage
- Demo HTTP launch video on youtube
- Setup from source
- Community
- Credits
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 Nodejs:
traits | go2hx | nodejs |
---|---|---|
No externs needed | ️️✅ | ❌ |
Cross target | ✅ | ❌ |
Large ecosystem | ✅ | ✅ |
Thread support | ✅ | ❌ |
Production ready | ❌ | ✅ |
Has tests | ✅ | ❌ |
Can create a web server | ✅ | ✅ |
Underlying code statically typed | ️️✅ | ❌ |
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.
Supported: Hashlink.
Partially supported: Eval
Planned: C++, Jvm, Javascript.
Javascript can only be supported with a coroutine model.
- Download Haxe 5.0.0+
- Git needs to be installed
- Hxcpp haxe library is required for the compiler
haxelib git hxcpp https://github.com/haxefoundation/hxcpp
-
A C++ compiler is required to use Hxcpp, for more information.
-
Install the compiler
haxelib git go2hx https://github.com/go2hx/go2hx
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
Requires
- go2hx installed
- 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!
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);
}
Clone the repo using the recursive flag:
git clone --recursive https://github.com/go2hx/go2hx
cd go2hx
haxelib dev go2hx .
haxelib run go2hx build
Haxe #go2hx discord channel. Read the contributing guide for insights and information on adding code into the project.
A spiritual successor to Tardisgo written by Elliott Stoneham, A maintainer, mentor, and code contributor of this project, go2hx would not exist without him.