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

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

Open
1 task done
chingzio opened this issue Sep 16, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@chingzio
Copy link

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

  • I agree to follow this project's Code of Conduct
@chingzio chingzio added the bug Something isn't working label Sep 16, 2022
@Tensai75
Copy link

I second that. This should really be addressed.

@egorlepa
Copy link

egorlepa commented Mar 1, 2023

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
}

@chingzio
Copy link
Author

chingzio commented Mar 1, 2023

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`

@CodeMonkey80s
Copy link

You can also use triple quotes:

key = """some#thing"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants