Skip to content

AutomaticEnv function failing to read environment variables in Go code #1522

@pejeio

Description

@pejeio

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • I am not looking for support or already pursued the available support channels without success.
  • I have checked the troubleshooting guide for my problem, without success.

Viper Version

1.15.0

Go Version

1.20.1

Config Source

Environment variables

Format

Other (specify below)

Repl.it link

No response

Code reproducing the issue

package main

import (
	"fmt"
	"log"

	"github.com/spf13/viper"
)

type Config struct {
	FbClientId string `mapstructure:"FB_CLIENT_ID"`
}

func LoadConfig() (config Config, err error) {
	viper.AutomaticEnv()

	err = viper.Unmarshal(&config)
	if err != nil {
		return
	}
	return
}

func main() {
	type Config struct {
		FbClientId string `mapstructure:"FB_CLIENT_ID"`
	}

	cfg, err := LoadConfig()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(cfg.FbClientId)
}

Expected Behavior

After running this command, I expect fmt.Print(cfg.FbClientId) to log 123:

export FB_CLIENT_ID="123" && go run main.go

Actual Behavior

There is no output in the console.

Steps To Reproduce

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions