I'd use godotenv in this case. True, it will require a .env file with a
path variable in it and true it won't be as having consts but it will do
the job.

import (
    "os"
"github.com/joho/godotenv"
)
func init() {
    if os.Getenv("MODE") != "PROD" {
        godotenv.Load()
    }
}

// ...
// Then later
path := os.Getenv("MY_PATH")



On Thu, 1 Dec 2022 at 10:39 hey...@gmail.com <hey....@gmail.com> wrote:

> I'm writing a command line program in go, and it needs to access some
> predefined external files like /usr/lib/name/file. What would be a good way
> to allow specify the constant path in compile time?
>
> I see two options:
>
> 1. go generate
> 2. -ldflags="-X 'main.Path=..."
>
> Both seem to be flawed. For #1, the blog clearly stated that it's designed
> for package author, and not for clients (and go generate doesn't support
> passing arguments to //go:generate anyways). For #2, the path can't be a
> constant.
>
> Is there a convention on how this problem should be solved or external
> paths should always be passed in as arguments in go?
>
> --
> 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/e5029b93-a3d9-4256-ab1c-a83d3b35c579n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/e5029b93-a3d9-4256-ab1c-a83d3b35c579n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAKX-YRAxaJ1J%2BJzZGsdtQ_oX73xh%2BmQSeNZ6t8%2BwXPCUBFLcVA%40mail.gmail.com.

Reply via email to