Golang Code

Get and Set Environment Variables

· 86 words · 1 minute read #env #environment #get #os #set #var #variable #variables

Environment variables are a useful way to work with sensitive information (think passwords) which your code needs to work with, but don’t need to be checked into your code base. With Go, you can both get and set these details using the os library.

package main

import (

func main() {
    // Outputs your 'visual' env var (in my case 'vim' (set in ~/.bashrc))

    // Create your own environment variable
    os.Setenv("Site", "GoLangCode")

    // Equals empty string if doesn't exist

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below. Thanks for reading!