Я хотел бы добавить метод во вложенную экспортируемую структуру, который используется в экспортированной структуре.
У меня есть []*ldap.Entry
тип, возвращаемый ldap.Search (). Записи
ldap.Entry
тип состоит из Attributes []*EntryAttribute
. Моя цель - добавить дополнительный метод в ldap.EntryAttribute
, например, MarshalJSON
Я могу добавить дополнительный код прямо в пакет ldap
, и он будет работать, как я ожидаю. Но это грязный путь:
// EntryAttribute holds a single attribute
type newEntryAttribute struct {
// Name is the name of the attribute
Name string
// Values contain the string values of the attribute
Values []string
// ByteValues contain the raw values of the attribute
ByteValues [][]byte
}
// Print outputs a human-readable description
func (e EntryAttribute) MarshalJSON() ([]byte, error) {
b := newEntryAttribute(e)
b.ByteValues = nil
return json.Marshal(b)
}
Как я могу сделать это более элегантно?