Logo

Using envvault with Go

Note: Please ensure that you have completed the previous steps

Prerequisites

  • Go installed on your system
  • envvault CLI tool installed
  • An existing Go project

Usage

Running Your Application

To run your Go application with environment variables from envvault:

$ envvault run --env=dev go run main.go

Caching Environment Variables

For better performance, you can cache your environment variables:

$ envvault run --env=dev -c -- go run main.go

Example Implementation

Here is how to set up a basic Go web server with envvault:

// main.go
package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    // Your environment variables are automatically loaded
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    dbURL := os.Getenv("DB_URL")

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello from envvault!")
    })

    fmt.Printf("Server running on port %s\n", port)
    http.ListenAndServe(":" + port, nil)
}