-
I have a struct with a package main
import (
"bytes"
"fmt"
"os"
"github.com/dop251/goja"
)
type document struct {
Field bytes.Buffer
}
func newDocument() *document {
d := &document{}
// I'd like to have this on the javascript side:
// d.Field.Write([]byte("foo"))
return d
}
func (d *document) Check() {
// Goal: should print "foo"
fmt.Println(`~~> d.Field`, d.Field.String())
}
func main() {
runtime := goja.New()
gjNewDoc := func(call goja.FunctionCall) goja.Value {
return runtime.ToValue(newDocument())
}
runtime.Set("newdoc", gjNewDoc)
_, err := runtime.RunString(`
var d = newdoc();
d.Field = "foo";
d.Check();
`)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
dop251
Sep 25, 2024
Replies: 1 comment 1 reply
-
Just replace d.Field = "foo"; with d.Field.Write("foo") |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
pgundlach
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just replace
with