HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoModerate

Reading environment variables of various types

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
readingenvironmentvariablestypesvarious

Problem

I need to extract some data from the environment variables using Go. This data can be string, boolean or integer, so I ended up writing three functions.

package main
import (
    "fmt"
    "os"
    "strconv"
)

func getStrEnv(key string) string {
    val := os.Getenv(key)
    if val == "" {
        panic(fmt.Sprintf("some error msg"))
    }
    return val
}

func getIntEnv(key string) int {
    val := getStrEnv(key)
    ret, err := strconv.Atoi(val)
    if err != nil {
        panic(fmt.Sprintf("some error"))
    }
    return ret
}

func getBoolEnv(key string) bool {
    val := getStrEnv(key)
    ret, err := strconv.ParseBool(val)
    if err != nil {
        panic(fmt.Sprintf("some error"))
    }
    return ret
}


Which works, but in a language like Python, I would just create one function getEnv(key, type_var), which would go through various passes depending on the type_var provided. Is there a way to produce similar result with Go?

Solution

Go is statically typed. Every variable has a static type, that is, exactly one type known and fixed at compile time. You are not in dynamically typed Python anymore.

You could use reflection, BUT DON'T!


Common difficulties with
reflection


If people are new to Go, they shouldn't be using reflection at all.


-rob

Your functions are specializations of os.Getenv. Revise the function names to getenvStr, getenvInt, and getenvBool.


os.Getenv retrieves the value of the environment variable named
by the key. It returns the value, which will be empty if the variable
is not present.

There is often nothing wrong when an environment variable is empty. Instead of panicing, return a named error.

For example,

package main

import (
    "errors"
    "os"
    "strconv"
)

var ErrEnvVarEmpty = errors.New("getenv: environment variable empty")

func getenvStr(key string) (string, error) {
    v := os.Getenv(key)
    if v == "" {
        return v, ErrEnvVarEmpty
    }
    return v, nil
}

func getenvInt(key string) (int, error) {
    s, err := getenvStr(key)
    if err != nil {
        return 0, err
    }
    v, err := strconv.Atoi(s)
    if err != nil {
        return 0, err
    }
    return v, nil
}

func getenvBool(key string) (bool, error) {
    s, err := getenvStr(key)
    if err != nil {
        return false, err
    }
    v, err := strconv.ParseBool(s)
    if err != nil {
        return false, err
    }
    return v, nil
}

func main() {}


References:

Package reflect

The Laws of Reflection

Code Snippets

package main

import (
    "errors"
    "os"
    "strconv"
)

var ErrEnvVarEmpty = errors.New("getenv: environment variable empty")

func getenvStr(key string) (string, error) {
    v := os.Getenv(key)
    if v == "" {
        return v, ErrEnvVarEmpty
    }
    return v, nil
}

func getenvInt(key string) (int, error) {
    s, err := getenvStr(key)
    if err != nil {
        return 0, err
    }
    v, err := strconv.Atoi(s)
    if err != nil {
        return 0, err
    }
    return v, nil
}

func getenvBool(key string) (bool, error) {
    s, err := getenvStr(key)
    if err != nil {
        return false, err
    }
    v, err := strconv.ParseBool(s)
    if err != nil {
        return false, err
    }
    return v, nil
}

func main() {}

Context

StackExchange Code Review Q#108563, answer score: 12

Revisions (0)

No revisions yet.