ini icon indicating copy to clipboard operation
ini copied to clipboard

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

Open zzt741 opened this issue 1 year ago • 0 comments

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

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

zzt741 avatar Oct 15 '24 02:10 zzt741