go - 有没有办法在 golang 中实现这样的目标?
问题描述
当前代码如下所示(简化版)。
const (
loginURL = "http://test.xxx.com"
authURL = "http://test.xxx.com"
tokenURL = "http://test.xxx.com"
)
err := login(loginURL)
err = auth(authURL)
err = token(tokenURL)
现在我想根据不同的情况更改 URL。
const test(
loginURL = "http://test.xxx.com"
authURL = "http://test.xxx.com"
tokenURL = "http://test.xxx.com"
) // test block
const dev(
loginURL = "http://dev.xxx.com"
authURL = "http://dev.xxx.com"
tokenURL = "http://dev.xxx.com"
) // dev block
// if test, use test URLs; if dev, use dev URLs.
err := login(loginURL)
err = auth(authURL)
err = token(tokenURL)
有没有办法实现上述目标?还是更好的方法?
解决方案
我建议您为此使用环境变量
var (
loginURL = os.Getenv("LOGIN_URL")
authURL = os.Getenv("AUTH_URL")
tokenURL = os.Getenv("TOKEN_URL")
)
err := login(loginURL)
err = auth(authURL)
err = token(tokenURL)
推荐阅读
- angular - 表格列的多个值上的角度过滤器管道
- python - 我已经尝试安装 tensorflow 两天了......它似乎安装但我无法运行它
- linux - 无与伦比的'。设置别名时出错
- angular - 如何对 ngfor 中的按钮进行单元测试
- python - Python 3 相当于 Bash 的“读取 -rs -t1 -n1”命令来轮询/等待用户的字符?
- r - 使用 lapply 在嵌套的多变量列表上应用函数
- python - 使用 cgi 字段存储发布到数据库时无法获取属性
- sql-server - 有没有办法检查 NOEXEC 状态?
- reactjs - ReactJS 中的 SignalR - 在不同组件中使用相同的 hubConnection
- flutter - 如何在没有无限循环的情况下调用 setState?