如何将Go及其资源文件打包成WASM?

huangapple go评论87阅读模式
英文:

How to bundle Go and its asset files into WASM?

问题

我有一个使用一些 JSON 文件(大约 15 个)作为数据的 Go 程序。我想将它编译成 WebAssembly 文件。

我该如何做呢?

注意:我知道我可以通过我的 JS 代码将这些 JSON 值传递给 WASM,但如果可以有效地将它们与 WASM 打包在一起,我不想这样做。

编辑:最后我们尝试了 https://pkg.go.dev/embed,正如 NobbyNobbs 提到的,它完美地工作了。

英文:

I have a Go program that uses some json files (like 15) for data. I want to compile it into a WebAssembly file.

How can I do this?

Note: I know I can just pass those json values from my JS code to WASM, but I don't want that if I can effectively bundle them with WASM.

EDIT: lastly we tried https://pkg.go.dev/embed as NobbyNobbs mentioned, and it's working perfectly.

答案1

得分: 1

如果您想将任何类型的文件/目录与编译代码(如二进制文件/wasm)捆绑在一起,有一个很棒的Golang库packr。它易于使用,当您将源代码编译为二进制文件或WebAssembly时,packr会加载文件/目录,并以与之前相同的方式工作。

这里我在项目的根目录中使用了***main.go,还有一个用于存储JSON数据的目录(如/jsondata/mydata.json***)。

main.go

package main

import (
    "fmt"

    "github.com/gobuffalo/packr/v2"
)

func main() {
    box := packr.New("myBox", "./jsondata")

    s, err := box.FindString("mydata.json")
    if err != nil {
        fmt.Println("🚀 ~ file: main.go ~ line 14 ~ funcmain ~ err : ", err)
    }
    fmt.Println(s)
}

/jsondata/mydata.json

{
	"glossary": {
		"title": "example glossary",
		"GlossDiv": {
		"title": "S"
		}
	}
}

然后使用go run main.go运行代码。
如果将其编译为二进制文件,json文件将与二进制代码一起发布。
go build main.go

如果将其编译为wasm,该文件夹将以相同的方式运行。
GOOS=js GOARCH=wasm go build -o main.wasm main.go

第二种方法(使用"embed")

使用标准库中的embed库。

这里我在项目的根目录中使用了***main.go,并在同一目录中创建了一个文件(如/sample.json***)。

main.go

package main

import (
  "embed"
)

//go:embed sample.json
var f embed.FS

func main(){
  data, _ := f.ReadFile("sample.json")
  print(string(data))
}

/sample.json

{
	"glossary": {
		"title": "example glossary",
		"GlossDiv": {
		"title": "S"
		}
	}
}

然后使用go run main.go运行代码。
如果将其编译为二进制文件,json文件将与二进制代码一起发布。
go build main.go

如果将其编译为wasm,该文件夹将以相同的方式运行。
GOOS=js GOARCH=wasm go build -o main.wasm main.go

英文:

if you want to bundle any type of file/directory with compiled code (like binary/wasm) , there is a awesome Golang library packr . It's easy to use and when you compile the source code to binary or webassembly packr loads the file/directory and works the same way as before.

here i am using main.go in the root directory of the project. and a directory to store json data (like /jsondata/mydata.json)

main.go

package main

import (
    "fmt"

    "github.com/gobuffalo/packr/v2"
)

func main() {
    box := packr.New("myBox", "./jsondata")

    s, err := box.FindString("mydata.json")
    if err != nil {
        fmt.Println("🚀 ~ file: main.go ~ line 14 ~ funcmain ~ err : ", err)
    }
    fmt.Println(s)

}

/jsondata/mydata.json

{
	"glossary": {
		"title": "example glossary",
		"GlossDiv": {
		"title": "S"
		}
	}
}

than run the code using go run main.go.
if you compiled it to the binary the json file ships with the binary code
go build main.go

if you compiled it to the wasm , the folder will behave the same way
GOOS=js GOARCH=wasm go build -o main.wasm main.go

Second method (Using "embed")

use embed library from standard library .

here i am using main.go in the root directory of the project. and creating a file in same directory (like /sample.json)

main.go

package main

import (
  "embed"
)

//go:embed sample.json
var f embed.FS

func main(){
data, _ := f.ReadFile("sample.json")
print(string(data))

}

/sample.json

{
	"glossary": {
		"title": "example glossary",
		"GlossDiv": {
		"title": "S"
		}
	}
}

than run the code using go run main.go.
if you compiled it to the binary the json file ships with the binary code
go build main.go

if you compiled it to the wasm , the folder will behave the same way
GOOS=js GOARCH=wasm go build -o main.wasm main.go

huangapple
  • 本文由 发表于 2022年10月18日 23:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/74113979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定