Golang viper cannot read configuration

843 Views Asked by At

I've been trying reading configuration by viper, while it always panic nil pointer reference(i've been set the config path and config name).

Then i tried to make a execution point on it and it tells me :

unreadable: could not read string at 0x8 due to Only part of a ReadProcessMemory or WriteProcessMemory request was completed.

execution point error info

Here is the config directory: Config directory

This is how i set the path:enter image description here

I'm almost crazy of it!!! It has been bothers me for 2 days (plz excuse me of lacking the experience of golang... I'm a new learner and thanks for your help very, very much!)

1

There are 1 best solutions below

1
On

I tried the following structure:

|main.go
|Config
|   config.go
|   config.yaml

With the following lines in the file config.go:

viper.AddConfigPath(".")
viper.SetConfigName("config") // Register config file name (no extension)
viper.SetConfigType("yaml")  // Look for specific type
viper.ReadInConfig()
version := viper.Get("server.version")
fmt.Println(version)

And my configuration file contains:

server:
  version: 1.0.0

In this case, my version is nil. But, if I set v.AddConfigPath("./Config")

viper.AddConfigPath("./Config")
viper.SetConfigName("config") // Register config file name (no extension)
viper.SetConfigType("yaml")  // Look for specific type
viper.ReadInConfig()
version := viper.Get("server.version")
fmt.Println(version)

Then my version is 1.0.0.

Your path depends on where is your main.go and not where is your config.go.

If you still have problems, it could also be due to bad rights on the file. You can still try to do a chmod 777 [your configuration file] to check if you code didn't need more right to read the file.