-
|
With the following code I get an error: My goal is to use a method I cannot change. How would I work around this issue? I guess (as always) I am missing something obvious. package main
import (
"fmt"
"os"
"github.com/dop251/goja"
)
type document struct {
}
func (d *document) Foo(r []rune) {
fmt.Println(r)
}
func newDocument() *document {
d := &document{}
return d
}
func main() {
runtime := goja.New()
gjNewDoc := func(call goja.FunctionCall) goja.Value {
v := runtime.ToValue(newDocument())
return v
}
runtime.Set("newdoc", gjNewDoc)
_, err := runtime.RunString(`
var d = newdoc();
d.Foo("Hello")
`)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
dop251
Oct 2, 2024
Replies: 1 comment 1 reply
-
|
There is no automatic conversion from In your specific case you could expose a method to convert a string to []rune, i.e. runtime.Set("toRunes", func toRunes(s string) []rune {
return []rune(s)
})
// ...
_, err := runtime.RunString(`
var d = newdoc();
d.Foo(toRunes("Hello"))
`) |
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
There is no automatic conversion from
stringto[]rune, mainly because unfortunatelyruneis a type alias forint32and therefore they can't be distinguished at runtime. And if your function expects[]int32it does not necessarily mean it expects[]rune.In your specific case you could expose a method to convert a string to []rune, i.e.