loading...

Configuring environmental variables for Go with ~/.bash_profile

cskonopka profile image Christopher Konopka Updated on ・1 min read

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

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

export API_KEY="KEY"
export API_SECRET="SECRET"

Reload the variables using source.

source ~/.bash_profile

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
}

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")

Print out the APIkey and APIsecret using fmt.

fmt.Println(APIkey, APIsecret)

Posted on by:

cskonopka profile

Christopher Konopka

@cskonopka

Christopher Konopka is a multidisciplinary artist that focuses on the cross section of human-computer interaction, analog video synthesis and experiential learning.

Discussion

markdown guide