tredoe / go2js Goto Github PK
View Code? Open in Web Editor NEW[DEPRECATED] Translator from Go to JavaScript
License: Mozilla Public License 2.0
[DEPRECATED] Translator from Go to JavaScript
License: Mozilla Public License 2.0
Is that possibale for translate js to Go using this project?
Example:
func test() {
a := 5
fmt.Println(a)
for i := 0; i < 1; i += 1 {
a := 6
fmt.Println(a)
}
fmt.Println(a)
}
Go and js have different results here.
This code crashes the translator:
type let struct {
this *this
}
type this struct {
this []*this
let
}
func (this *this) apply(function this, call let) {
this.this = append(this.this, &function)
this.this = append(this.this, call.this)
}
Thanks for this project. Quick question: is there a way to tell go2js to emit some raw JS code from within a .go file?
If not, how do you recommend writing wrappers for existing JS libraries? For example, I'd like to wrap a library like jQuery and encapsulate the Go->JS bindings in a single place. Then I can call the wrapped Go version in a type safe manner.
Thoughts?
From README,
Status
Inactive | Incomplete
Sorry to not be able to continue with this project for some time, but I have to
work very hard in third projects.Although I follow being interested in be able to use Go in the browser.
Since you say you're interested in being able to use Go in the browser, I wanted to ask...
I wanted to ask, are you aware of GopherJS project? It has gotten pretty far, including support for goroutines, channels, and select recently.
https://github.com/gopherjs/gopherjs
https://groups.google.com/forum/#!forum/gopherjs
I'd like to use go2js to emit Javascript that works in IE, FF, Chrome, etc. The const keyword throws an exception in IE.
A few possibilities:
This test
type test struct {
value string
}
func (t *test) get() string {
return t.value
}
Currently translates to:
function test(value) {
this.value=value
}
test.prototype.get = function() {
return this.value;
}
It would be probably better to translate into:
function test(value) {
this.value=value
}
test.prototype.get = function() {
var t = this;
return t.value;
}
If you are at some point doing anon. functions then handling of this can get quite complicated otherwise.
Not a high priority for me, but probably something to consider. It looks like it would be pretty easy to support as it just impacts the wrapper code around the generated .js file.
mustache.js has a good example:
https://github.com/janl/mustache.js/blob/master/mustache.js
(function (root, factory) {
if (typeof exports === "object" && exports) {
module.exports = factory; // CommonJS
} else if (typeof define === "function" && define.amd) {
define(factory); // AMD
} else {
root.Mustache = factory; // <script>
}
}(this, (function () {
// body code here
var exports = {};
// bind all exported identifiers -- similar to current output
return exports;
}())));
This would work with require.js, CommonJS (e.g. node.js), and plain <script> tags.
I also thought about go to js translator, pretty awesome idea.
What about this kind of channels/gorountines implementation:
var Go = (function () {
var result = {package: {}};
/* chan */
var chan = function () {
this.getCallbackStack = [];
this.putCallbackStack = [];
};
chan.prototype.put = function (value, callback) {
if (this.getCallbackStack.length == 0) {
this.putCallbackStack.push({value: value, callback: callback});
} else {
var getCallback = this.getCallbackStack.pop().callback;
if (getCallback) {
getCallback(value);
}
if (callback) {
callback();
}
}
};
chan.prototype.get = function (callback) {
if (this.putCallbackStack.length == 0) {
this.getCallbackStack.push({callback: callback});
} else {
var elt = this.putCallbackStack.pop();
var putCallback = elt.callback, value = elt.value;
if (callback) {
callback(value);
}
if (putCallback) {
putCallback();
}
}
};
result.chan = chan;
/* goroutine */
result.go = function (callback, arguments) {
arguments = arguments || [];
setTimeout(function () {
callback.apply(null, arguments);
}, 0);
};
result.println = function (value) {
console.log(value);
};
return result;
})();
Go.package["time"] = {
Second: 1000,
Sleep: function (duration, callback) {
setTimeout(callback, duration)
}
};
And example of use:
// package main
//
// import "time"
//
// func main() {
// ch := make(chan bool)
// go func() {
// println("Locked")
// <-ch
// println("Unlocked")
// ch <- true
// }()
// time.Sleep(time.Second*2)
// ch <- true
// <- ch
// }
(function () {
var time = Go.package["time"];
var ch = new Go.chan();
Go.go(function () {
Go.println("Locked");
ch.get(function (value) {
Go.println("Unlocked");
ch.put(true);
});
});
time.Sleep(time.Second*2, function () {
ch.put(true, function () {
ch.get();
});
});
})();
This in this code channels have capacity equal to 1, but can be easily tuned to support buffered channels.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.