-
Notifications
You must be signed in to change notification settings - Fork 17.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: embed: conditional embedding support #44484
Comments
Can you just use a build tag? |
@ianlancetaylor Is there any option that can be used for such use cases? Build tag is OK, but it seems currently not supported. |
@kesuskim You should be able to put the
Then build with |
@jayconrod That will do! Thank you 😄 Maybe it will be enough to have some documents or blogs about it, I should make writing on my blog. |
I think we should re-open this because Why can't I use this code instead? var somefile string
if embedAllowed {
//go:embed somefile.txt
somefile
} |
@kesuskim can you re-open please? |
You can achieve similar with build tag. Here is an example for it.
package public
import "embed"
var Box embed.FS
var IsEmbedded bool
func init() {
Box = ebox
}
// +build embed
package public
import "embed"
//go:embed * index.html
var ebox embed.FS
func init() {
IsEmbedded = true
}
// +build !embed
package public
import "embed"
var ebox embed.FS
func init() {
IsEmbedded = false
}
package main
import (
"fmt"
"log"
"net/http"
"myproject/public"
)
const PORT = 8080
var httpRoot http.FileSystem
func main() {
if public.IsEmbedded {
httpRoot = http.FS(public.Box)
} else {
httpRoot = http.Dir("public/")
}
log.Printf("server is listening on :%d", PORT)
http.Handle("/", http.FileServer(httpRoot))
if err := http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil); nil != err {
log.Fatalln(err)
}
} To use $ go build -tags embed |
I think having multiple options on doing one thing (in this case, conditional embed) could be very confusing and diverged in the end, and IMHO it is not a Go-way. |
Yeah, I know and I'm using it, but my question is about not using |
@frederikhors I don't understand what it means to write var somefile string
if embedAllowed {
//go:embed somefile.txt
somefile
} In order to implement that, the file somefile.txt must always be embedded in the binary. So I don't understand what you are asking for. |
I'm asking to only embed file if |
When we build the binary, we have to decide whether to embed the file or not. We can't postpone that decision until the binary is run. But when we build the binary, we don't know whether |
Really nice to see
embed
finally gets arrived to standard library!Yet I believe, conditional embedding would be desirable in some use cases, seems there aren't any.
I was using packr beforehand to achieve this kind of behavior; mainly because when I want to build for container image, I do not want to embed files (mainly frontend built assets files) packed into binary because it will increase memory usage a lot.
I think it would be enough
//go:embed
might just ignore embedding by giving buildflags or environment variables in build time.The text was updated successfully, but these errors were encountered: