Settings global conditional variables in Go

huangapple go评论83阅读模式
英文:

Settings global conditional variables in Go

问题

我想在Go的全局包级别上根据环境变量的值来解析和设置一个变量,这样我就不必每次在实用函数中检查它(因为变量只需在运行时声明一次)。例如,我想要实现的是(Go伪代码):

import (
    "fmt"
    "os"
    "strconv"
)

// 这段代码可以读取MYVAR的值(true/false)
var myvar string = os.Getenv("MYVAR")

// 显然这对于Go来说太多了
var myvarbool, _ = strconv.ParseBool(myvar)

// 检查值的实用函数
func mycheck() {
    if myvarbool {
        fmt.Print("MYVAR is true")
    }
}

这是一个库包,所以没有main()函数来进行此类设置,但我希望能够在库中的其他函数中使用mycheck(),而不必每次调用mycheck()时都读取和解析MYVAR。

英文:

I want to parse and set a variable conditionally in Go at the global package level based on the value of of an ENV var, so that I don't have to check for it every time in a utility function (as the variable would be declared once at run time). For example, what I want to accomplish is (Go pseudo-code):

import (
    "fmt"
    "os"
    "strconv"
)

// This works to read the value of MYVAR (=true/false)
var myvar string = os.Getenv("MYVAR")

// Apparently this is too much for Go
var myvarbool, _ = strconv.ParseBool(myvar)

// Utility function to check for value
func mycheck() {
    if myvarbool {
        fmt.Print("MYVAR is true")
    }
}

This is a library package, so doesn't have a main() function to do this kind of setup, but I want to be able to use mycheck() in other functions in the library, and don't want to have to read and parse MYVAR every time mycheck() is called.

答案1

得分: 6

你要翻译的内容如下:

要实现你想要做的事情,一种方法是在init()函数中处理环境变量,所以以下代码可以实现:

package main

import (
    "fmt"
    "os"
    "strconv"
)

var (
    myvar string = os.Getenv("MYVAR")
    myvarbool bool
)

func init() {
    myvarbool, _ = strconv.ParseBool(myvar)
}

// 检查值的实用函数
func mycheck() {
    if myvarbool {
        fmt.Print("MYVAR is true")
    }
}

Playground

英文:

One way to accomplish what you're looking to do would be to process the environment variable in an init() function, so something like the following would work:

package main

import (
    "fmt"
    "os"
    "strconv"
)

var (
    myvar string = os.Getenv("MYVAR")
    myvarbool bool
)

func init() {
    myvarbool, _ = strconv.ParseBool(myvar)
}

// Utility function to check for value
func mycheck() {
    if myvarbool {
        fmt.Print("MYVAR is true")
    }
}

Playground

答案2

得分: 4

我不确定你遇到了什么问题,你在原始帖子中的代码是有效的,你也可以将其写成一行,像这样:

var myvarbool, _ = strconv.ParseBool(os.Getenv("MYVAR"))

playground

英文:

I'm not sure what problem you had, your code from the OP is valid, also you can have it in one line like:

var myvarbool, _ = strconv.ParseBool(os.Getenv("MYVAR"))

<kbd>playground</kbd>

答案3

得分: 0

在经过几分钟的思考后,我意识到我可以创建一个包装函数来完成这个工作,并调用该函数(再次强调,以下是伪代码,没有额外的检查):

var myenvbool bool = myenv("MYVAR")

func myenv(envvar string) bool {
    myenvvalue := os.Getenv(envvar)
    myenvbool, _ := strconv.ParseBool(myenvvalue)
    return myenvbool
}

func checkenv() {
    if myenvbool {
        fmt.Print("myenvbool is true")
    }
}
英文:

After actually thinking about this for a couple of minutes, I've realised I can just create a wrapper function to do the work, and call that instead (again, largely pseudo-code without extra checks):

var myenvbool bool = myenv(&quot;MYVAR&quot;)

func myenv(envvar string) bool {
    myenvvalue := os.Getenv(envvar)
    myenvbool, _ := strconv.ParseBool(myenvvalue)
    return myenvbool
}

func checkenv() {
    if myenvbool {
        fmt.Print(&quot;myenvbool is true&quot;)
    }
}

huangapple
  • 本文由 发表于 2015年2月15日 20:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/28526138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定