Reading and Writing Environment Variables in Go: An End-to-End Example

Environment variables are a powerful way to configure your applications without having to hardcode values in your code. In this article, we will walk through an end-to-end example of how to read and write environment variables in Go.

Getting Started

First, let's create a new Go file to work with. Open up your favorite text editor and create a new file called main.go. In this file, we will create a simple program that reads an environment variable and prints it out to the console.

package main

import (
    "fmt"
    "os"
)

func main() {
    value, ok := os.LookupEnv("MY_ENV_VAR")
    if ok {
        fmt.Println("The value of MY_ENV_VAR is:", value)
    } else {
        fmt.Println("MY_ENV_VAR is not set")
    }
}

In this program, we are using the os package to read the value of an environment variable called MY_ENV_VAR. We are also using the fmt package to print out the result to the console.

Running the Program

To run this program, set the MY_ENV_VAR environment variable to a value of your choice and execute the following command:

go run main.go

This will print out the value of MY_ENV_VAR to the console. If MY_ENV_VAR is not set, the program will print out a message saying that it is not set.

Setting Environment Variables

Now that we know how to read environment variables, let's modify our program to set a new environment variable. Open up main.go and replace the contents with the following code:

package main

import (
    "fmt"
    "os"
)

func main() {
    err := os.Setenv("MY_NEW_ENV_VAR", "hello")
    if err != nil {
        fmt.Println("Error setting MY_NEW_ENV_VAR:", err)
        return
    }

    value, ok := os.LookupEnv("MY_NEW_ENV_VAR")
    if ok {
        fmt.Println("The value of MY_NEW_ENV_VAR is:", value)
    } else {
        fmt.Println("MY_NEW_ENV_VAR is not set")
    }
}

In this program, we are using the os package to set a new environment variable called MY_NEW_ENV_VAR with a value of "hello". We are then using the os package again to read the value of MY_NEW_ENV_VAR and print it out to the console.

Running the Program

To run this program, execute the following command:

go run main.go

This will set a new environment variable called MY_NEW_ENV_VAR and print out its value to the console.

Conclusion

In this article, we walked through an end-to-end example of how to read and write environment variables in Go. By leveraging the os package, we can easily read and write environment variables in our Go programs. With this knowledge, you can create powerful, configurable programs that can be easily customized without having to modify your code.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!