ini icon indicating copy to clipboard operation
ini copied to clipboard

If the configuration item of my ini configuration file contains #, it will cause an error in reading the configuration.

Open chingzio opened this issue 3 years ago • 4 comments

Version

1.67.0

Describe the bug

If the configuration item of my ini configuration file contains #, it will cause the configuration to be read incorrectly. For example, my MySQL password is "xxx#123", and the obtained parameter is "xxx".

To reproduce

ini file

[database] DbPassWord = "XXX#123"

config.go

file, err := ini.Load("config/app.ini") DbPassWord = file.Section("database").Key("DbPassWord").MustString("") fmt.Println("passwd:"DbPassWord)

This outputs passwd: "XXX

Expected behavior

Configuration items containing # can be read correctly.

Additional context

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

chingzio avatar Sep 16 '22 03:09 chingzio

I second that. This should really be addressed.

Tensai75 avatar Jan 30 '23 07:01 Tensai75

same problem, can't have values with # workaround:

func extractValue(key *ini.Key) string {
	value := key.Value()
	comment := key.Comment
	if len(comment) == 0 {
		return value
	}
	if idx := strings.LastIndex(comment, "\n"); idx != -1 {
		comment = comment[idx+1:]
	}
	if len(comment) > 0 && comment[0] == '#' {
		value += comment
	}
	return value
}

egorlepa avatar Mar 01 '23 07:03 egorlepa

I found a solution, you can use ` instead of " in the ini file, and you can read the configuration correctly. for example:

[database] DbPassWord = `XXX#123`

chingzio avatar Mar 01 '23 07:03 chingzio

You can also use triple quotes:

key = """some#thing"""

CodeMonkey80s avatar Jan 12 '24 10:01 CodeMonkey80s