DEV Community

Christopher Konopka
Christopher Konopka

Posted on • Edited on

2 3

Configuring environmental variables for Go with ~/.bash_profile

Before sharing a project that uses credentials, it is important to show a user how to save their credentials within the bash_profile. The reasoning for this is security, and it makes variables globally accessible within the architecture. The example is specifically for OSX and Linux.

Open the ~/.bash_profile using your favorite text editor using Terminal.

nano ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Create a new variable and export it. Save and exit.

export API_KEY="KEY"
export API_SECRET="SECRET"
Enter fullscreen mode Exit fullscreen mode

Reload the variables using source.

source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Create a new Go program and name it main.js. Add the fmt and os libraries.

package main

import(
  "fmt"
  "os"
)

func main(){
    // ... code here
}
Enter fullscreen mode Exit fullscreen mode

Use the os library's Getenv function to retrieve the environmental variable and print them out.

APIkey := os.Getenv("API_KEY")
APIsecret := os.Getenv("API_SECRET")
Enter fullscreen mode Exit fullscreen mode

Print out the APIkey and APIsecret using fmt.

fmt.Println(APIkey, APIsecret)
Enter fullscreen mode Exit fullscreen mode

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
arham_abiyan profile image
Arham Awal Abiyan • Edited

Hello, I think you made a mistake about the file name here

Create a new Go program and name it main.js. Add the fmt and os libraries.

it should be main.go

very helpful post btw, thanks

Retry later
👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay