英文:
Environment variable returns an empty string from a function in golang library
问题
目前我正在尝试使用 spotify wrapper API 来进行 Golang 编程,并且现在遇到了获取 SPOTIFY_ID 和 SPOTIFY_SECRET 环境变量的问题。我在一个 .env 文件中设置了我的环境变量,并且该 .env 文件存储在我的项目目录中。
我正在使用 godotenv 这个库来从我的 .env 文件中加载环境变量,并且它们成功地从我的服务器二进制文件中输出。然而,当我调用 Spotify API 模块中的一个函数时,我注意到环境变量返回了一个空字符串,但是它在我的 .env 变量中是设置好的。我认为这可能是因为 Spotify API 模块所在的 go 目录位于我的项目文件夹之外,所以我怀疑作用域可能会引起问题,但我并不完全确定,因为如果我在终端中设置环境变量,就没有问题。我希望我可以在 .env 文件中设置它。
英文:
So currently I am trying to use the spotify wrapper API for golang and am now facing issues with retrieving the environment for my SPOTIFY_ID and SPOTIFY_SECRET. I set my environment variables in a .env file and the .env file is stored in my project directory.
I am using the library godotenv to load the env variables from my .env, and they are successfully getting outputted from my server binary. However, when I make a call to a function in the Spotify API module, I notice the environment returns an empty string, but it is set in my .env variables. I think it's because the module for the Spotify API module is in a go directory that is located outside of my project folder, so I suspect the scoping may be causing an issue but I am not entirely sure, because if I set the environment variables in terminal I have no issues. I was hoping I could set it in a .env file instead.
答案1
得分: 2
一个进程的环境变量是从它的父进程继承而来的。Go进程和Spotify API模块的父进程可能是终端。
所以,如果你想通过Go设置环境变量,并且Spotify API模块可以获取到它,也许你只能通过使用Go来启动Spotify API模块。
英文:
A process's env is from its father process. Go process and the Spotify API module 's father process probably is terminal.
So if you want to set env through go and the Spotify API module can get it. Maybe, You can only start the Spotify API moduleby using go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论