github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xos/env.go (about)

     1  package xos
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  func EnvBool(key string) (bool, error) {
    10  	if val, ok := os.LookupEnv(key); ok {
    11  		return strconv.ParseBool(val)
    12  	}
    13  	return false, nil
    14  }
    15  
    16  func EnvInt64(key string) (int64, error) {
    17  	if val, ok := os.LookupEnv(key); ok {
    18  		return strconv.ParseInt(val, 10, 64)
    19  	}
    20  	return 0, nil
    21  }
    22  
    23  func EnvInt64s(key string) ([]int64, error) {
    24  	if val, ok := os.LookupEnv(key); ok {
    25  		items := strings.Split(val, ",")
    26  		ints := make([]int64, 0, len(items))
    27  		for _, item := range items {
    28  			i, err := strconv.ParseInt(item, 10, 64)
    29  			if err != nil {
    30  				return nil, err
    31  			}
    32  			ints = append(ints, i)
    33  		}
    34  		return ints, nil
    35  	}
    36  	return nil, nil
    37  }
    38  
    39  func EnvString(key string) (string, error) {
    40  	return os.Getenv(key), nil
    41  }
    42  
    43  func EnvStrings(key string) ([]string, error) {
    44  	return strings.Split(os.Getenv(key), ","), nil
    45  }