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

Section.Key passing a empty key name will return a nil #358

Open
1 task done
zzt741 opened this issue Oct 15, 2024 · 0 comments
Open
1 task done

Section.Key passing a empty key name will return a nil #358

zzt741 opened this issue Oct 15, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@zzt741
Copy link

zzt741 commented Oct 15, 2024

Version

1.66.6

Describe the bug

https://github.com/go-ini/ini/blob/main/section.go#L169

// Key assumes named Key exists in section and returns a zero-value when not.
func (s *Section) Key(name string) *Key {
	key, err := s.GetKey(name)
	if err != nil {
		// It's OK here because the only possible error is empty key name,
		// but if it's empty, this piece of code won't be executed.
		key, _ = s.NewKey(name, "")
		return key
	}
	return key
}

注释有点没看懂,为什么传入空键值,代码不会执行?我传入了空键值,导致返回了一个nil,由于没有检查,导致了panic。按理说这里不应该返回nil吧。

To reproduce

	ini, _ := ini.Load("file")
	section, _ := ini.GetSection("Product")
	key := section.Key("")
	fmt.Println(key)

Expected behavior

是否可以改为传入空键值时不返回nil,或者说为什么不支持空字符串作为键值?

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@zzt741 zzt741 added the bug Something isn't working label Oct 15, 2024
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

3 participants
@zzt741 and others