💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 示例 在本节中,我们将了解如何将 Go 程序编译为 WebAssembly 代码。 `toWasm.go`的 Go 代码如下: ```Go package main import ( "fmt" ) func main() { fmt.Println("Creating WebAssembly code from Go!") } ``` 这里要注意的是此代码并没有用到 WebAssembly,并且 toWasm.go 可以单独编译和执行,这意味着它没有与 WebAssembly 相关的外部依赖关系。 创建 WebAssembly 代码所需执行的最后一步是执行以下命令: ```shell $ GOOS=js GOARCH=wasm go build -o main.wasm toWasm.go $ ls -l total 4760 -rwxr-xr-x 1 mtsouk staff 2430633 Jan 19 21:00 main.wasm -rw-r--r--@ 1 mtsouk staff 100 Jan 19 20:53 toWasm.go $ file main.wasm main.wasm: , created: Thu Oct 25 20:41:08 2007, modified: Fri May 28 13:51:43 2032 ``` 因此,在第一个命令中通过`GOOS`和`GOARCH`的值告诉 Go 创建 WebAssembly 代码。如果未输入正确的`GOOS`和`GOARCH`值,则编译将不会生成 WebAssembly 代码。