I have two Go modules, let's name them example.com/a
and example.com/b
.
Let this be example.com/a
's go.mod
:
module example.com/a
go 1.12
require (
example.com/b v0.4.2
)
In example.com/b
's root directory, there is a file named data.yaml
. example.com/a
needs to autogenerate some code as part of its build process. This autogeneration needs to read data.yaml
.
How can I in the directory of example.com/a
query for the path of example.com/b
to read that file? I know that after downloading, the module will be somewhere in (go env GOPATH)/pkg/mod
but I don't know how the path will be constructed from there as it contains some !
characters that are not part of the import path. I hoped that there is some subcommand of go mod
or go list
that will output the path, but I haven't found it in the documentation.
I have thought about including data.yaml
in Go code via go-bindata
(yes I'm aware of //go:embed
but I don't want to require Go 1.16 for now) but then I would only have access at run-time when I need it at compile-time.
example.com/b
export a function which usesruntime.Caller
to figure out its file's location and using that constructs the absolute path to the yaml file and returns it. Then theexample.com/a
could invoke this function to get that location. I do use this approach myself wherea
importsb
,a
invokes a function ofb
, that function reads css/js/html files from its own module's folder hierarchy and then writes copies of those files intoa
. – Ingamarexample.com/b
so I can't call a function from there. – Thorfinngithub.com/BurntSushi/toml
dir location into/Users/mkopriva/Work/go/pkg/mod/github.com/!burnt!sushi/[email protected]
-- so maybe you can use that rule to make sure to find the package in the correct place. – Ingamarpackages.NeedModule
flag, which will load info on the package's module, including it's directory, if there is any. – Ingamar