登录
首页 >  Golang >  Go问答

在Go中使用 genrules 来嵌入资源

来源:stackoverflow

时间:2024-03-10 12:30:29 351浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《在Go中使用 genrules 来嵌入资源》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我需要在二进制文件中包含一些生成的文件(例如 go-swagger 的输出)。如果没有 bazel,您可以使用 go:generatego:embed 结合使用:

package demo
//go:generate swagger generate spec -w . -o ./openapi.json

import "embed"

// content contains openapi.json file generated via go-swagger from spec
//go:embed openapi.json
var content embed.fs

我正在尝试对 bazel 做同样的事情。作为一个简单的测试,我有这个 build.bazel 文件:

genrule(
    name = "hellodata",
    srcs = ["hello.go"],
    outs = ["hello.txt"],
    cmd = "cat $(srcs) | tr a-za-z n-za-mn-za-m > $@",
)

go_library(
    name = "hello",
    srcs = ["hello.go"],
    importpath = "wiggy.net/hello",
    visibility = ["//visibility:public"],
    embedsrcs = [":hellodata"],
)

hello.go 看起来像这样:

package hello

import (
    _ "embed"
    "io"
)

//go:embed hello.txt
var greeting []byte

func hello(out io.writer) error {
    _, err := out.write(greeting)
    return err
}

这里的目的是让 hello 输出它自己源代码的 rot13。当我尝试编译它时,它成功生成 hello.txt (位于 bazel-out/darwin_arm64-fastbuild/bin/hello.txt ),但编译器找不到它:

❯ bazel build //...                              
INFO: Analyzed 5 targets (0 packages loaded, 0 targets configured).
INFO: Found 5 targets...
ERROR: /Users/wichert/Hack/bzl/BUILD.bazel:14:11: GoCompilePkg hello.a failed: (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_arm64 -src hello.go -embedsrc bazel-out/darwin_arm64-fastbuild/bin/hello.txt -importpath wiggy.net/hello ... (remaining 12 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
compilepkg: /private/var/tmp/_bazel_wichert/e7573342ee9452df4c3dfa671d399a16/sandbox/darwin-sandbox/76/execroot/__main__/hello.go:8:12: could not embed hello.txt: no matching files found
INFO: Elapsed time: 0,112s, Critical Path: 0,04s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

正确答案


回答我自己的问题:技巧是使用 genrule 生成要嵌入的文件,然后使用 go_embed_data 嵌入它们。一个作品 build.bazel 看起来像这样:

genrule(
    name = "hellodata",
    srcs = ["hello.go"],
    outs = ["hello.txt"],
    cmd = "cat $(srcs) | tr a-za-z n-za-mn-za-m > $@",
)

go_embed_data(
    name = "hello_embed",
    src = ":hellodata",
    package = "hello",
    var = "greeting",
)

go_library(
    name = "hello",
    srcs = [
        "hello.go",
        ":hello_embed",
    ],
    importpath = "wiggy.net/hello",
    visibility = ["//visibility:public"],
)

我注意到命令行中的 -embedsrc bazel-out/darwin_arm64-fastbuild/bin/hello.txt 导致您问题中的失败操作,因此我预感在我的机器上尝试了等效的操作:

//go:embed bazel-out/k8-fastbuild/bin/hello.txt
var greeting []byte

这似乎有效。

这不太好,因为配置信息嵌入到源文件中(实际上,在您的 mac 计算机上它是 darwin_arm64-fastbuild 而在我的 linux 计算机上它是 k8-fastbuild),所以现在您的源代码是人为平台相关的。

看起来这个功能相对较新(https://github.com/bazelbuild/rules_go/issues/2775https://github.com/bazelbuild/rules_go/issues/2986)。我会向rules_go 提交有关此问题的问题。

似乎还有 go_embed_data ,其行为可能有所不同:

https://github.com/bazelbuild/rules_go/blob/master/docs/go/extras/extras.md#go_embed_data

理论要掌握,实操不能落!以上关于《在Go中使用 genrules 来嵌入资源》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>