zogot / go-embed-standards-layout

An example of using the golang-standards project layout and the go:embed directive.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go:embed and the golang-standards project layout

While experimenting with Go and trying out the go:embed directive to serve a VueJS application I was running into an issue with trying to stick to the golang-standards project layout

This is because go:embed doesn't let you embed from parent directories and since in this specific scenario I was trying to have multiple services split into cmd/ subfolders this seemed to be at odds with each other.

Patterns may not contain ‘.’ or ‘..’ or empty path elements, nor may they begin or end with a slash.

source

This led me to a deep search into some other projects that may have ran into a similar problem and I found this commit on github https://github.com/lawrencejones/pgsink/commit/3b7c58f79e21b6b6dfffcdd8e7403bb1523747cb

Realising that by having a go package made of the web folder, I could reference it, so combining with using build tags to either serve by the os package when developing or serve by embed when building I had a solution that worked well.

Considering that I found many more questions asking how to do it when I searched, I figured I would provide an example.

Run

Run with using the embed.FS

go run -tags prod cmd/http/main.go

Run with the os.Dir

go run cmd/http/main.go

Browse to http://127.0.0.1:8080 to see the index.html

About

An example of using the golang-standards project layout and the go:embed directive.


Languages

Language:Go 100.0%