添加3种方法或通用方法[关闭]

What is better, having 3 methods to get different properties:

func (s *Something) GetSections() {
  var sections []Section
  // code to get sections and append to sections
}

func (s *Something) GetValues() {
  var values []Value
  // code to get values and append to values
}

func (s *Something) GetKeys() {
  var keys []Key
  // code to get keys and append to keys
}

Or one method that receives what should get:

func (s *Something) GetProperties(propertyName string) {
  // code to get the desire property.
}

I'm talking here about the best practice involving creating a method to access the internal data of a struct.

I'm not sure if this is opinion based question, if it yes, I'm happy to delete it.

The only reason to use the second one would be if the property is only known at runtime. Then there is a legitimate reason to parse the name and select the value according to its name.

However, since you suggest that the first one is an option, Go with that. It prevents you from having conditional logic at a place where it unnecessary. Otherwise, you'd strictly have to handle the cases where propertyName is not any of the valid options; introducing a possible error where there should not be any.

And as suggested in the comments, the idiomatic name for getters are without Get. So Sections() instead of GetSections().