iterator

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 0 Imported by: 1

README

Go - Iterator

Go Report Card License MIT Go Doc

This package provides an iterator interface and a slice iterator implementation.

Install

go get -u github.com/danielgatis/go-iterator

And then import the package in your code:

import "github.com/danielgatis/go-iterator"
Example

An example described below is one of the use cases.

package main

import (
	"fmt"

	"github.com/danielgatis/go-iterator"
)

func main() {
	numbers := []int{1, 2, 3, 4, 5}
	iter := iterator.NewIterator(numbers)

	for iter.HasNext() {
		value := iter.GetNextOrDefault(0)
		fmt.Println(value)
	}
}

❯ go run main.go
License

Copyright (c) 2023-present Daniel Gatis

Licensed under MIT License

Buy me a coffee

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[T any] interface {
	// contains filtered or unexported methods
}

Iterator is the interface that wraps the basic methods for an iterator.

type SliceIterator

type SliceIterator[T any] struct {
	// contains filtered or unexported fields
}

SliceIterator is the implementation of Iterator for a slice.

func NewIterator

func NewIterator[T any](items []T) *SliceIterator[T]

NewIterator returns a new instance of SliceIterator.

func (*SliceIterator[T]) GetNext

func (u *SliceIterator[T]) GetNext() (T, bool)

GetNext returns the next item in the slice.

func (*SliceIterator[T]) GetNextOrDefault

func (u *SliceIterator[T]) GetNextOrDefault(d T) T

GetNextOrDefault returns the next item in the slice or the default value if there is no next item.

func (*SliceIterator[T]) HasNext

func (u *SliceIterator[T]) HasNext() bool

HasNext returns true if there is a next item to retrieve.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL