When trying to change the static content information set in the toml file to one using environment variables error question that occurred
Put the corresponding code first
// .env variables
STATICS=[["web", "/var/www/ichain-admin-react"],["static", "static"]]
// source code
func serveStaticFiles(engine *gin.Engine) {
statics := os.Getenv("STATICS")
for i := 0; i < len(statics); i++ {
url, root := statics[i][0], statics[i][1]
if !path.IsAbs(root) {
root = path.Join(config.ExecutedPath, root)
}
engine.Static(url, root)
}
}
invalid operation: cannot index statics[i] (value of type byte)
I didn't find any articles that would help me much
Thank you
os.Getenv
returns a string
which you can't index in the way you're trying to. But since the value has the same format as a json array of arrays, you should be able to use the encoding/json
package to parse the string and decode it into a Go slice.
Something like this should work:
func serveStaticFiles(engine *gin.Engine) {
statics := os.Getenv("STATICS")
slice := [][]string{}
if err := json.Unmarshal([]byte(statics), &slice); err != nil {
panic(err)
}
for i := 0; i < len(slice); i++ {
url, root := slice[i][0], slice[i][1]
if !path.IsAbs(root) {
root = path.Join(config.ExecutedPath, root)
}
engine.Static(url, root)
}
}