Issue getting .env vars golang
xepobopa
HOBBYOP

a year ago

All my code on GoLang works well, but i cannot get .env vars in code.
I do it like this

func GetDotenv(names ...string) ([]string, error) {
    if err := godotenv.Load(); err != nil {
        return []string{}, fmt.Errorf("error trying to get .env var: %w", err)
    }

    var vars []string
    for idx, name := range names {
        vars[idx] = os.Getenv(name)
    }

    return vars, nil
}

and i do not get anything. I also tried to specify .env path , but it also didnt work. But error error trying to get .env var: do not fire, it just return me empty list

Solved

7 Replies

xepobopa
HOBBYOP

a year ago

37b514d7-42c5-4dd1-baf6-ff6cd324227b


mikexie360
PRO

a year ago

I used a different go library

    endpointSecret := os.Getenv("STRIPE_WEBHOOK_SIGNING_SECRET") // Set this in .env
    event, err := webhook.ConstructEvent(body, sigHeader, endpointSecret)

xepobopa
HOBBYOP

a year ago

Thank you, i will try it tomorrow


a year ago

you dont need to use any libray, only os.Getenv


a year ago

we do not inject variables as a .env file, they are injected directly into the environment


xepobopa
HOBBYOP

a year ago

Ok, i understood, thank you a lot


a year ago

!s


Status changed to Solved brody 11 months ago


Loading...