bldsh / dive

dive in to learn
Apache License 2.0
0 stars 0 forks source link

go: slice: string: byte: unsafe: error: unwrap #13

Open AmitKumarDas opened 2 weeks ago

AmitKumarDas commented 2 weeks ago
// Join returns an error that wraps the given errors.
// Any nil error values are discarded.
// Join returns nil if every value in errs is nil.
// The error formats as the concatenation of the strings obtained
// by calling the Error method of each element of errs, with a newline
// between each string.
//
// A non-nil error returned by Join implements the Unwrap() []error method.
func Join(errs ...error) error {
    n := 0
    for _, err := range errs {
        if err != nil {
            n++
        }
    }
    if n == 0 {
        return nil
    }
    e := &joinError{
        errs: make([]error, 0, n),
    }
    for _, err := range errs {
        if err != nil {
            e.errs = append(e.errs, err)
        }
    }
    return e
}

type joinError struct {
    errs []error
}
func (e *joinError) Error() string {
    // Since Join returns nil if every value in errs is nil, e.errs cannot be empty.
    if len(e.errs) == 1 {
        return e.errs[0].Error()
    }

    b := []byte(e.errs[0].Error()) // amit: initialize
    for _, err := range e.errs[1:] {
        b = append(b, '\n') // amit: the char i.e. '\n' helps separate the error list when the list is logged
        b = append(b, err.Error()...) // amit: notice how string is used as list of chars
    }
    // At this point, b has at least one byte '\n'. // amit: correct
    return unsafe.String(&b[0], len(b)) // amit: start from address of b to its whole length
}
func (e *joinError) Unwrap() []error {
    return e.errs
}