Categorygithub.com/golang-infrastructure/go-iterator
modulepackage
0.0.0-20230524171120-56988a9b127c
Repository: https://github.com/golang-infrastructure/go-iterator.git
Documentation: pkg.go.dev

# README

Golang Iterator

一、简介

Golang迭代器模式的定义与实现,用于在一些适合迭代器的场景使用。

二、安装

go get -u github.com/golang-infrastructure/go-iterator

三、Example

slice := []int{1, 2, 3, 4, 5}
iterator := NewSliceIterator(slice)
toSlice := ToSlice[int](iterator)
assert.Equal(t, slice, toSlice)

四、API

Iterator[T any]

// Iterator 表示一个迭代器,迭代器的元素类型是T
type Iterator[T any] interface {

	// Next 将迭代器的指针往后移动一个,同时返回指针当前指向的位置是否有元素
	Next() bool

	// Value 返回指针指向的元素,如果没有元素的话返回对应类型的零值
	Value() T
}

IteratorWithIndex[T any]

IteratorWithKey[T any]

ChainIterator[T any]

用来把多个迭代器组合为一个:

	iteratorA := NewRangeIterator(0, 5)
	iteratorB := NewRangeIterator(5, 10)

	iterator := NewChainIterator[int](iteratorA, iteratorB)
	slice := ToSlice[int](iterator)
	t.Log(slice)

ChannelIterator[T any]

用来把channel封装为迭代器:

	channel := make(chan int, 3)
	channel <- 1
	channel <- 2
	channel <- 3
	close(channel)

	iterator := NewChannelIterator(channel)
	slice := ToSlice[int](iterator)
	t.Log(slice)

CycleIterator[T any]

用来无限循环返回某个序列:

	iterator := NewCycleIterator(1, 2, 3)
	for i := 0; i < 100 && iterator.Next(); i++ {
		t.Log(iterator.Value())
	}

MapIterator[K comparable, V any]

用来迭代Map:

	m := make(map[string]int, 0)
	m["a"] = 1
	m["b"] = 2
	m["c"] = 3
	iterator := NewMapIterator(m)
	slice := ToSlice[*MapEntry[string, int]](iterator)
	t.Log(slice)

RangeIterator

用来返回一个范围:

	begin := -100
	end := 100
	iterator := NewRangeIterator(begin, end)
	slice := ToSlice[int](iterator)

	exceptedSlice := make([]int, 0)
	for begin < end {
		exceptedSlice = append(exceptedSlice, begin)
		begin++
	}

	assert.Equal(t, exceptedSlice, slice)

SliceIterator[T any]

用来把切片包装为迭代器:

	slice := []int{1, 2, 3, 4, 5}
	iterator := NewSliceIterator(slice)
	toSlice := ToSlice[int](iterator)
	assert.Equal(t, slice, toSlice)

# Functions

CastToIterator 转换为迭代器.
DoIterator 遍历一个迭代器.
No description provided by the author
No description provided by the author
IsIterator 判断给定的struct是否实现了迭代器.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewRangeIterator [begin, end)的左闭右开区间.
No description provided by the author
ToChannel 把迭代器转为channel.
ToSlice 把迭代器转为切片.

# Variables

No description provided by the author

# Structs

ChainIterator 把多个迭代器组合为一个迭代器,.
ChannelIterator 用于把一个channel封装为iterator.
CycleIterator 把一个给定的序列无限循环.
No description provided by the author
MapIterator 用于遍历一个Map的迭代器.
RangeIterator 范围递增的迭代器.
SliceIterator 用于把切片封装为迭代器.

# Interfaces

ErrorableIterator 表示一个迭代器,迭代器的元素类型是T,这个迭代器在遍历的时候允许发生错误.
Iterable 用于表示是可迭代的,拥有一个Iterator方法,调用时返回一个迭代器.
Iterator 表示一个迭代器,迭代器的元素类型是T.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author