dave / jsgo

GopherJS compiler, serving framework and CDN.
https://jsgo.io/<path>
MIT License
269 stars 11 forks source link

Adapt to console application #14

Closed suntong closed 6 years ago

suntong commented 6 years ago

Hi Dave,

A noob question -- How to adapt jsgo transpiled code for/to use within console?

Let me explain with a simple example: https://github.com/suntong/game24/blob/c8000136ce33497ff32d09e9d52fb33df6bb2399/str/game24/main.go#L19-L22

 func main() { 
    r := game24.Play(30) 
    fmt.Println(r) 
 } 

Suppose I've got what I want from line 20, and I'll change line 21 to console.log(r),

how can I write a index.js file that does the above two simple steps?

My ultimate goal is to use my jsgo transpiled code as lib for my console based nodejs program. I.e., I don't know JS enough to turn the code of

$load["something"] = function () {
$packages["something"] = (function() {
...

to nodejs based program that use module.exports and require("./something"); for module importing.

THX a lot!

dave commented 6 years ago

Hey, sorry for the delay on responding... I'm a bit hazy on what you want to actually achieve... You might have more luck asking this question over in the GopherJS slack channel.

To emit console.log("foo") with GopherJS you'd do something like js.Global.Get("console").Call("log", "foo")... You can see similar examples in the Interacting with the DOM section of the GopherJS documentation.

As far as using GopherJS JS code in a non-GopherJS application, this may be tricky. I've not done this myself, so I'm not an expert. You can check out the Providing library functions for use in other JavaScript code section in the GopherJS documentation. I think that will allow you to define a global JS object that can contain functions that you'll be able to call from JS.

I hope this answers your questions...

suntong commented 6 years ago

NP. Thanks for your detailed answer . I'll close this and ask in GopherJS slack channel instead. Thx!