如何在嵌入式结构类型之间共享方法?

时间:2019-05-24 05:35:38

标签: go

是否可以让memoryByteRanges继承TotalLength的方法LenSwapLessbyteRanges,而不必重新定义功能?

type byteRange struct {
    start int
    end   int
}

type byteRanges []*byteRange

func (brs byteRanges) Len() int {
    return len(brs)
}

func (brs byteRanges) Swap(i, j int) {
    brs[i], brs[j] = brs[j], brs[i]
}

func (brs byteRanges) Less(i, j int) bool {
    return brs[i].start < brs[j].start
}

func (brs byteRanges) TotalLength(i, j int) bool {
    l := 0
    for _, br := range brs {
        l = l + br.end - br.start
    }

    return l
}

type memoryByteRange struct {
    byteRange
    data []byte
}

type memoryByteRanges []*memoryByteRange

0 个答案:

没有答案