Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why OnConfigChange() not work? #703

Closed
dncmn opened this issue May 22, 2019 · 1 comment
Closed

why OnConfigChange() not work? #703

dncmn opened this issue May 22, 2019 · 1 comment

Comments

@dncmn
Copy link

dncmn commented May 22, 2019

I write in a golang test file.code following

func TestViper(t *testing.T) {
//func main() {
viper.SetConfigName("config")
viper.AddConfigPath("../config")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.SetConfigType("yaml")
var configuration c.Configurations

if err := viper.ReadInConfig(); err != nil {
	log.Fatal(err)
	return
}

err := viper.Unmarshal(&configuration)
if err != nil {
	log.Fatal(err)
	return
}
fmt.Println(configuration.Server.Port)
fmt.Println(configuration.GameServer)
fmt.Println(configuration.GOPATH)
fmt.Println(configuration.EXAMPLE_VAR)
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
	if in.Op == fsnotify.Write {
		if err := viper.Unmarshal(&configuration); err != nil {
			log.Fatal(err)
		}
		fmt.Println("write")
		log.Printf("%#v", configuration)
	}

})
select {}

}

#658 config.yaml
server:
port: 8080
gameServer:
host: http://127.0.0.1/
token: 789
GOPATH: "variable from config.yaml"
EXAMPLE_VAR: "path from config.yaml"

after run test method ,I modify token from "789" to "123456"

but I see nothing output

@dncmn dncmn closed this as completed May 22, 2019
@dncmn
Copy link
Author

dncmn commented May 22, 2019

wrong op.Name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant