PPysolate SpineREAD THE CODE · UNDERSTAND THE SYSTEM
课程与章节
源码索引 / CURRENT IMPLEMENTATION

learning/go-patterns/patterns/slice-boundary/slice_boundary.go

24 行 · 点击行号可定位;函数链接随声明位置更新,不手填行号。

跳到函数 / 声明
// Package sliceboundary demonstrates defensive copying at slice boundaries.
package sliceboundary

import "slices"

// Batch owns its items. The backing array is never exposed directly.
type Batch struct {
	items []string
}

// NewBatch copies items so later caller mutations cannot change the Batch.
func NewBatch(items []string) Batch {
	return Batch{items: slices.Clone(items)}
}

// Len reports the number of items.
func (b Batch) Len() int {
	return len(b.items)
}

// Items returns a copy so caller mutations cannot change the Batch.
func (b Batch) Items() []string {
	return slices.Clone(b.items)
}