我想嵌入隐藏的文件,在Go中嵌入包。例如 git 信息。


例如


package main


import _ "embed"


var (

    //go:embed .git/HEAD

    head string

)


func main() {

    print(head)

}

但它发生编译错误。


$ go build  

main.go:6:13: pattern .git/HEAD: cannot embed file .git/HEAD: in invalid directory .git

我尝试了这些方法,但没有成功。


建立符号链接并嵌入

用双引号转义和换行


添加

这到底是什么意思?files outside the package's module


这听起来不像是关于go模块的。


为什么下面的文件是好的,但不是?.git/*


$ mkir .hidden && echo "hello world" > .hidden/hello.txt

$ tree -a

.

|-- .hidden

|   `-- hello.txt  <- is package's module ?

|-- go.mod

|-- go_embed_git_version

`-- main.go

和 之间有什么区别?.git/HEAD.hidden/hello.txt