If the Go application reads environment variables, supply its secrets at deployment. If it requires config.yaml, commit only config.yaml.example and mount the real file at runtime.
When to use this guide
Check the sample and real configuration files in the repository. If fields such as database.password, api.token or jwt.secret contain live credentials, revoke the exposed values first and issue replacements.
Recommended structure
config.yaml.example # 샘플 설정만 포함, Git 커밋 가능
config.yaml # 실제 운영값, Git 커밋 금지
.gitignore # config.yaml 포함
Examples
server:
port: 8080
database:
host: prod-db.internal
user: app_user
password: super-secret-password
jwt:
secret: hardcoded-jwt-secret
Pattern A: Environment-variable injection
Use this option when you have few secrets, your deployment supports environment variables and the application can read each value without the YAML hierarchy.
config.yaml.example
server:
port: 8080
database:
host: ${DB_HOST}
user: ${DB_USER}
password: ${DB_PASSWORD}
jwt:
secret: ${JWT_SECRET}
Go code example
This code reads environment variables directly. yaml.Unmarshal does not expand ${ENV_VAR} automatically; a loader that reads the YAML above needs separate substitution logic.
package config
import (
"fmt"
"os"
)
type Config struct {
DBPassword string
JWTSecret string
}
func Load() (*Config, error) {
cfg := &Config{
DBPassword: os.Getenv("DB_PASSWORD"),
JWTSecret: os.Getenv("JWT_SECRET"),
}
if cfg.DBPassword == "" {
return nil, fmt.Errorf("DB_PASSWORD is not configured")
}
if cfg.JWTSecret == "" {
return nil, fmt.Errorf("JWT_SECRET is not configured")
}
return cfg, nil
}
Pattern B: Mount a config.yaml file
Use a mounted file if the application requires a fixed path or a nested YAML configuration. Supply the real file at a path such as /etc/app/config.yaml with a Kubernetes Secret volume, a CSI driver, Vault Agent or another protected delivery channel.
Repository file: config.yaml.example
server:
port: 8080
database:
host: prod-db.internal
user: app_user
password: "<runtime-provided>"
jwt:
secret: "<runtime-provided>"
Example runtime path
/etc/app/config.yaml
Go code example
package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type FileConfig struct {
Database struct {
Password string `yaml:"password"`
} `yaml:"database"`
JWT struct {
Secret string `yaml:"secret"`
} `yaml:"jwt"`
}
func LoadFromFile(path string) (*FileConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config file: %w", err)
}
var cfg FileConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse config file: %w", err)
}
if cfg.Database.Password == "" {
return nil, fmt.Errorf("database.password is not configured")
}
if cfg.JWT.Secret == "" {
return nil, fmt.Errorf("jwt.secret is not configured")
}
return &cfg, nil
}
The application must fail immediately if the file is missing or a required secret is empty.
Developer responsibilities
- Remove hardcoded development values. Read environment variables with
os.Getenv()or the configuration loader. For mounted files, document the path the application reads. - Stop startup when a required value is missing or empty. Keep full configuration objects out of logs.
Infrastructure and platform responsibilities
- Inject environment variables at deployment or mount the production file where only the application account can read it. Keep that file out of Git, Docker images and ConfigMaps.
- Define how to update environment variables or files after rotation and how to redeploy or reload the application so it picks up the new value.
Verification
- Verify that Git does not track the real
config.yamland thatconfig.yaml.examplecontains no production values. - Test startup with a required environment variable or file missing. The application should stop.
- Check logs, panic messages, configuration dumps and debug output for exposed values.
If a secret was exposed
- Revoke the exposed credential immediately and issue a replacement.
- Stop tracking
config.yamland add it to.gitignore. Past commits may still contain the value. Keep only keys and the expected structure in the sample. - Deploy the replacement through environment variables or a mounted file. On Kubernetes, choose environment variables or a Secret volume according to how the application reads its settings.