generics

package
v0.0.0-...-434faa1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

This failed experiment wanted to create a generic ForEach method. However you cannot cast. e.g., []int to []interface{} and need to use a chan interface{} to pass sequential data around. The result is just to slow to provide any value.

ForEach results:

BenchmarkForEach10-4      243576              5053 ns/op
BenchmarkForEach1k-4        3158            373238 ns/op
BenchmarkForEach1M-4           3         377289272 ns/op

Normal loop results:

BenchmarkLoop10-4        1625445               727 ns/op
BenchmarkLoop1k-4          19542             59471 ns/op
BenchmarkLoop1M-4             19          61522294 ns/op

Documentation

Overview

Package generics shows how to NOT use interfaces to emulate generics!

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForEach

func ForEach(ch <-chan interface{}, fn Operation) error

ForEach is too slow. This is just a demo. DON'T USE!

ForEach concurrently runs an `Operation` on the given items in a channel and waits for them to finish. If an error occurs it returns this error after waiting is done.

If Go had generics we could define a generic list type and would not have to rely on a channel.

Types

type Operation

type Operation func(index int, item interface{}) error

Operation defines an operation to run with each item.

Jump to

Keyboard shortcuts

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