英文:
Go WASM export functions
问题
我想创建一个.wasm
文件,在编译时仍然导出函数名。
package main
import (
"fmt"
)
func main() {
fmt.Println("Main")
}
func MyFunc() {
fmt.Println("MyFunc")
}
我使用以下命令进行构建
GOOS=js GOARCH=wasm go build -o main.wasm
这将生成wasm文件(很棒,Go原生支持wasm)。
但是使用wabt并进行对象转储会暴露这些函数。
Export[4]:
- func[958] <wasm_export_run> -> "run"
- func[959] <wasm_export_resume> -> "resume"
- func[961] <wasm_export_getsp> -> "getsp"
- memory[0] -> "mem"
我期望看到类似于
func[137] <MyFunc> -> "MyFunc"
有人知道如何在Go WASM中导出函数吗?
在Rust中,包括#[no_mangle]
和pub extern "C"
可以使函数在wasm-pack的输出中保持可用。我正在寻找类似于Go的方法。
英文:
I want to create a .wasm
file which still has the function names exported when compiled.
package main
import (
"fmt"
)
func main() {
fmt.Println("Main")
}
func MyFunc() {
fmt.Println("MyFunc")
}
I'm building with
GOOS=js GOARCH=wasm go build -o main.wasm
Which produces the wasm file (and awesome that Go targets wasm natively).
But using wabt and doing an object dump exposes these functions.
Export[4]:
- func[958] <wasm_export_run> -> "run"
- func[959] <wasm_export_resume> -> "resume"
- func[961] <wasm_export_getsp> -> "getsp"
- memory[0] -> "mem"
I'm expecting to see something like
func[137] <MyFunc> -> "MyFunc"
Does anyone know how to export functions in Go WASM?
In rust including #[no_mangle]
and pub extern "C"
keeps the function available in the output with wasm-pack. I'm looking for something similar with Go.
答案1
得分: 7
如果你计划在Go中编写大量的WASM代码,你可能想考虑使用TinyGo进行编译,它是一个用于嵌入式和WASM的Go编译器。
TinyGo支持//export <name>
或别名//go:export <name>
的注释指令,可以实现你所需的功能。
我将复制粘贴TinyGo WASM文档中的第一个示例:
package main
// This calls a JS function from Go.
func main() {
println("adding two numbers:", add(2, 3)) // expecting 5
}
// ...omitted
// This function is exported to JavaScript, so can be called using
// exports.multiply() in JavaScript.
//export multiply
func multiply(x, y int) int {
return x * y;
}
你可以使用以下命令构建它:tinygo build -o wasm.wasm -target wasm ./main.go
。
标准的Go编译器正在进行一个持续的开放讨论,讨论如何复制TinyGo的功能。简而言之,你可以通过将函数设置为JS全局命名空间来实现,使用js.Global().Set(...)
。
英文:
If you plan to write a lot of WASM in Go, you might want to consider compiling with TinyGo, which is a Go compiler for embedded and WASM.
TinyGo supports a //export <name>
or alias //go:export <name>
comment directive that does what you're looking for.
I'm copy-pasting the very first example from TinyGo WASM docs:
package main
// This calls a JS function from Go.
func main() {
println("adding two numbers:", add(2, 3)) // expecting 5
}
// ...omitted
// This function is exported to JavaScript, so can be called using
// exports.multiply() in JavaScript.
//export multiply
func multiply(x, y int) int {
return x * y;
}
And you build it with: tinygo build -o wasm.wasm -target wasm ./main.go
.
<hr>
The standard Go compiler has an ongoing open discussion about replicating TinyGo feature. The tl;dr seems to be that you can achieve this by setting funcs to the JS global namespace, with the js.Global().Set(...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论