env Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/env/ Sun, 23 Feb 2020 06:41:06 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 env Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/env/ 32 32 159787465 Check if a environment variable is set in Go (Golang) https://vikasboss.github.io/check-environment-variable-set-go/ https://vikasboss.github.io/check-environment-variable-set-go/#respond Sun, 23 Feb 2020 06:40:55 +0000 https://vikasboss.github.io/?p=1441 os.LookupEnv function can be used to check whether a particular environment variable is set or not. It returns a bool which is true if the given env variable set otherwise false. Let’s...

The post Check if a environment variable is set in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
os.LookupEnv function can be used to check whether a particular environment variable is set or not. It returns a bool which is true if the given env variable set otherwise false. Let’s see a working code:

package main

import (
    "fmt"
    "log"
    "os"
)

func main() {
    //Set env a to b
    err := os.Setenv("a", "x")
    if err != nil {
        log.Fatal(err)
    }

    val, present := os.LookupEnv("a")
    fmt.Printf("a env variable present: %t\n", present)
    fmt.Println(val)

    val, present = os.LookupEnv("b")
    fmt.Printf("b env variable present: %t\n", present)
}

Output:

a env variable present: true
x
b env variable present: false

The post Check if a environment variable is set in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/check-environment-variable-set-go/feed/ 0 1441