Firstly, there must be no space between "//" and "go:embed". Otherwise it's 
just treated as a regular comment, and you get an empty fs with no error.  
(Perhaps "go vet" could be enhanced to catch this?)

After fixing this, you'll see the underlying error:
main.go:10:12: pattern public: no matching files found

For an explanation see https://pkg.go.dev/embed#hdr-Directives

"The patterns are interpreted relative to the package directory containing 
the source file"

These means you'll need to move your public/ directory inside the cmd/ 
directory. (You can't use a symlink; this causes the embed process to fail 
with error "cannot embed irregular file")

After that, it should be OK :-)

On Saturday, 2 September 2023 at 09:28:11 UTC+1 Sankar wrote:

> I have the following go code where I try to embed a html file from a 
> public directory and running into HTTP 404.  What am I doing wrong ? 
> Relevant code snippet:
>
>
> // go:embed public
> var public embed.FS
>
> func main() {
>     // cd to the public directory
>     publicFS, err := fs.Sub(public, "public")
>     if err != nil {
>         log.Fatal(err)
>     }
>     http.Handle("/embedded/", http.FileServer(http.FS(publicFS)))
>
>     http.Handle("/public/",
>         http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
>
>     log.Fatal(http.ListenAndServe(":8080", nil))
> }
>
> The entire golang file and the html file are available in 
> https://github.com/psankar/static-serve
>
> I have tried adding even a `http.StripPrefix` for the `/embedded/` path 
> also but even that does not help. Any pointers on what I am doing wrong ?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/788df31e-daa1-4d92-8d0a-d52e136bb8den%40googlegroups.com.

Reply via email to