是否可以在Go语言中实现类似的功能?

作者:佚名

时间:2024-03-29

来源:stackoverflow

浏览:458

当前代码如下所示(简化版本)。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。consttest(

golang学习网今天将给大家带来《是否可以在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)

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

热门专题

更多
返回顶部