0

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!)

protob
  • 3,317
  • 1
  • 8
  • 19
  • 1
    Hi, please add the error text you get here, so we can find out in which part of the code you get the error and why. – meshkati Mar 04 '23 at 20:23
  • And please **do not** post images of text or links to image of text. Include the source and any output or errors directly in your question, as text, formatted as a code sample. – larsks Mar 04 '23 at 22:22
  • Please excuse me, I’m new here in stackoverflow~~ I will notice it next time and thank u for replying – Chris Zhang Mar 05 '23 at 14:30

1 Answers1

0

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.

Loïc Madiès
  • 277
  • 1
  • 5
  • 19
  • I’ve tried it, but it still doesn’t work. I make an execution point at viper.New() and it tells me the viper is a nil pointer, i think the problem is that the viper isn’t initialized. While I cannot understand that I’ve used the code above in the project past and it didn’t occur any problems. But in this project it panic… crazy now – Chris Zhang Mar 05 '23 at 14:26