Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ jobs:

- name: Extern tests
run: cd hx2go-extern && haxe Testbed.hxml

- name: Test examples
run: haxe scripts/test_examples.hxml

- name: Go stdlib include test
run: haxe scripts/include_gen.hxml
Expand Down
7 changes: 7 additions & 0 deletions examples/asciigraph/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import go.github_com.guptarohit.Asciigraph;

function main() {
var data:Array<Float> = [3, 4, 9, 6, 2, 4, 5, 8, 5, 10, 2, 7, 2, 5, 6];
var graph:String = Asciigraph.plot(data);
trace("\n" + graph);
}
6 changes: 6 additions & 0 deletions examples/asciigraph/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-cp examples/asciigraph
-m Main
-L hx2go
-D go-lib=github.com/guptarohit/asciigraph
--custom-target go=output/examples/asciigraph
--cmd go -C output/examples/asciigraph/main run .
43 changes: 43 additions & 0 deletions examples/ebiten/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import go.github_com.hajimehoshi.ebiten.v2.Ebitenutil;
import go.github_com.hajimehoshi.ebiten.v2.Ebiten;
import go.github_com.hajimehoshi.ebiten.v2.Image;
import go.Pointer;
import go.GoInt;
import go.Error;
import go.Tuple;

class Game {

public function new() {
return;
}

@:go.Export
public function draw(screen: Pointer<Image>): Void {
Ebitenutil.debugPrint(screen, "Hello, World!");
}

var count = 0;

@:go.Export
public function update(): Error {
count++;
if (count > 60 * 5) {
Sys.exit(0);
}
return null;
}

@:go.Export
@:go.Tuple("screenWidth", "screenHeight")
public function layout(outsideWidth: GoInt, outsideHeight: GoInt): Tuple<{ screenWidth: GoInt, screenHeight: GoInt }> {
return { screenWidth: 640, screenHeight: 480 };
}

}

function main() {
Ebiten.setWindowSize(640, 480);
Ebiten.setWindowTitle("Hello, World!");
Ebiten.runGame(new Game()).sure();
}
6 changes: 6 additions & 0 deletions examples/ebiten/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-cp examples/ebiten
-m Main
-L hx2go
-D go-lib=github.com/hajimehoshi/ebiten/v2/...
--custom-target go=output/examples/ebiten
--cmd go -C output/examples/ebiten/main run .
7 changes: 7 additions & 0 deletions examples/go_lua/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import go.github_com.shopify.go_lua.Lua;

function main() {
var l = Lua.newState();
Lua.openLibraries(l);
Lua.doString(l, 'print("hello world")').sure();
}
6 changes: 6 additions & 0 deletions examples/go_lua/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-cp examples/go_lua
-m Main
-L hx2go
-D go-lib=github.com/Shopify/go-lua/...
--custom-target go=output/examples/go_lua
--cmd go -C output/examples/go_lua/main run .
41 changes: 41 additions & 0 deletions examples/gorm/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import go.Os;
import go.gorm_io.gorm.Model;
import go.gorm_io.Gorm;
import go.gorm_io.driver.Sqlite;
import sys.FileSystem;

function main() {
if (FileSystem.exists("test.db")) {
FileSystem.deleteFile("test.db");
}

var db = Gorm.open(Sqlite.open("test.db")).sure();
db.autoMigrate(new Product());

db.create(new Product("D42", 100));

var product = new Product();
db.first(product, 1); // find product with integer primary key
db.first(product, "hx_field_code = ?", "D42");

// update single field
db.model(product).update("hx_field_price", 200);
// update multiple fields
db.model(product).updates(new Product("F42", 200));
db.model(product).updates({hx_field_price: 200, hx_field_code: "F43"});
// delete
db.delete(product, 1);

}

@:structInit
class Product {
@:go.Tag('gorm:"embedded"')
public var model:Model;
public var code:String;
public var price:UInt;
public function new(code="", price=0) {
this.code = code;
this.price = price;
}
}
7 changes: 7 additions & 0 deletions examples/gorm/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-cp examples/gorm
-m Main
-L hx2go
-D go-lib=gorm.io/gorm/...
-D go-lib=gorm.io/driver/sqlite/...
--custom-target go=output/examples/gorm
--cmd go -C output/examples/gorm/main run .
20 changes: 20 additions & 0 deletions examples/gtk/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import go.github_com.diamondburned.gotk4.pkg.gtk.v4.Gtk;
import go.github_com.diamondburned.gotk4.pkg.gtk.v4.Application;

class Main {
static function main() {
var app = Gtk.newApplication("com.github.diamondburned.gotk4-examples.gtk4.simple", 0b0);
app.connectActivate(() -> {
activate(app);
});
app.run([]);
}

static function activate(app:Application) {
var window = Gtk.newApplicationWindow(app);
window.setTitle("gotk4 Example");
window.setChild(Gtk.newLabel("Hello from Go!"));
window.setDefaultSize(400, 300);
window.show();
}
}
6 changes: 6 additions & 0 deletions examples/gtk/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-cp examples/gtk
-m Main
-L hx2go
-D go-lib=github.com/diamondburned/gotk4/pkg/...
--custom-target go=output/examples/gtk
--cmd go -C output/examples/gtk/main run .
13 changes: 13 additions & 0 deletions examples/http_fileserver/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import go.net.Http;
import go.net.http.Dir;

function main() {
var port = ":8082";
trace('http file server: http://localhost$port');

var dir:Dir = ".";
var err = Http.listenAndServe(port, Http.fileServer(dir));
trace(err);
// NOTE: the server will continue to run until interrupted.
// Polite shutdown is beyond the scope of this simple example.
}
6 changes: 6 additions & 0 deletions examples/http_fileserver/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-cp examples/http_fileserver
-m Main
-L hx2go
# as this code calls the Go standard library, there is no need for a '-D go-lib=' entry here
--custom-target go=output/examples/http_fileserver
--cmd go -C output/examples/http_fileserver/main run .
19 changes: 19 additions & 0 deletions examples/miqt/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import go.github_com.mappu.miqt.Qt;
import go.Os;

function main() {
Qt.newQApplication(Os.args);

var btn = Qt.newQPushButton3("Hello World!");
btn.setFixedWidth(320);

var counter = 0;
btn.onPressed(() -> {
counter++;
btn.setText('You have clicked the button $counter time(s)!');
});

btn.show();

Qt.qApplication_Exec();
}
6 changes: 6 additions & 0 deletions examples/miqt/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-cp examples/miqt
-m Main
-L hx2go
-D go-lib=github.com/mappu/miqt/...
--custom-target go=output/examples/miqt
--cmd go -C output/examples/gorm/miqt run .
16 changes: 16 additions & 0 deletions scripts/TestExamples.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function main() {
for (dir in sys.FileSystem.readDirectory("examples")) {
// skip
switch dir {
case "miqt", "gtk", "http_fileserver", "ebiten":
continue;
default:
}
// run
var command = 'haxe examples/$dir/build.hxml';
Sys.println(command);
var code = Sys.command(command);
if (code != 0)
Sys.exit(code);
}
}
3 changes: 3 additions & 0 deletions scripts/test_examples.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-cp scripts
-m TestExamples
--interp
Loading