I encountered a problem while writing a Go project. I try to follow standard project layout in which all .json files are in configs (or config) directory.
The problem itself is that I don’t understand the best way to write this .json file using go:embed (I use it on the advice of my teacher). He advised me not to do things like os.Open("../../configs/example.json")
because it only works if the .json is nearby. But he didn’t show me how, in this case, without adding ../../
to the path, you can get to this .json file using embed. All processing in internal.
How can this be done in internal without breaking the project structure without moving .json from configs?
Inside internal I have package jsonproc, in which I tried to do something like:
//go:embed configs/*
//go:embed example.json
var config []byte
It didn’t work out that way. Everything works if .json is placed next to this Go file in jsonproc. That way I can just write:
//go:embed example.json
var config []byte
But it’s incorrect
bitmagic52 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.