Documentation ¶
Overview ¶
Package osenv provides convenient functions to access environment variables.
Copyright 2022 Rustam Gilyazov. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var TimeFormat = time.RFC3339
TimeFormat specifies the supported time format for Time values. The default value is RFC3339 time format.
Functions ¶
func Secret ¶
func Secret[T SupportedTypes](key string, defavlt T) T
Secret returns the value of the environment variable with the name `key`. If the environment variable with name KEY not found, it returns the `defavlt` value. The environment variable is unset after the value is retrieved.
Example ¶
os.Setenv("SECRET_VALUE", "hidden fact") fmt.Printf("os.Getenv before: %q\n", os.Getenv("SECRET_VALUE")) aSecret := Secret("SECRET_VALUE", "xxx") fmt.Printf("aSecret variable contains: %q\n", aSecret) fmt.Printf("os.Getenv after: %q\n", os.Getenv("SECRET_VALUE"))
Output: os.Getenv before: "hidden fact" aSecret variable contains: "hidden fact" os.Getenv after: ""
func Value ¶
func Value[T SupportedTypes](key string, defavlt T) T
Example ¶
fmt.Printf("OSENV_BOOL: %v\n"+ "OSENV_DURATION: %s\n"+ "OSENV_FLOAT: %.7f\n"+ "OSENV_INT: %d\n"+ "OSENV_INT64: %d\n"+ "OSENV_STRING: %s\n"+ "OSENV_TIME: %s\n", Value("OSENV_BOOL", true), Value("OSENV_DURATION", 60*time.Second), Value("OSENV_FLOAT", math.Pi), Value("OSENV_INT", math.MaxInt32), Value("OSENV_INT64", math.MaxInt64), Value("OSENV_STRING", "default string value"), Value("OSENV_TIME", time.Date(2020, 12, 31, 23, 59, 59, 0, time.UTC)), )
Output: OSENV_BOOL: true OSENV_DURATION: 1m0s OSENV_FLOAT: 3.1415927 OSENV_INT: 2147483647 OSENV_INT64: 9223372036854775807 OSENV_STRING: default string value OSENV_TIME: 2020-12-31 23:59:59 +0000 UTC
Types ¶
Click to show internal directories.
Click to hide internal directories.