Skip to content

Convert string to rune slice #616

Answered by dop251
pgundlach asked this question in Q&A
Discussion options

You must be logged in to vote

There is no automatic conversion from string to []rune, mainly because unfortunately rune is a type alias for int32 and therefore they can't be distinguished at runtime. And if your function expects[]int32 it does not necessarily mean it expects []rune.

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"))
`)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pgundlach
Comment options

Answer selected by pgundlach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants