Skip to content

gopher-utils/go-collections

Repository files navigation

go-collections

License Codecov

Basic collections with Go using generics.

Table of Contents

Installation

go get github.com/gopher-utils/go-collections

Usage

Below is an example of how you can use go-collections in your project.

package main

import (
	"fmt"

	"github.com/gopher-utils/go-collections/collections"
)

func main() {
	// Create a new empty list of integers with capacity 10
	emptyList := collections.NewList[int](10)
	// Insert a number
	emptyList.Add(1)
	fmt.Println(emptyList) // Prints [1]

	// Create a list of integers with repeating ones of size 5
	repeatingList := collections.RepeatingList(1, 5)
	fmt.Println(repeatingList) // Prints [1,1,1,1,1]

	// Create a list from array
	fromArrayList := collections.ToList([]int{2, 3, 4})
	fmt.Println(fromArrayList) // Prints [2,3,4]

	// Concatenate two lists together
	emptyList.Extend(fromArrayList)
	fmt.Println(emptyList) // Prints [1,2,3,4]

	// Sort a list of integers
	newList := collections.ToList([]int{4, 3, 2})

	// call sort with a comparision function that takes two values and returns an integer of 0,1 or -1
	newList.Sort(func(a, b int) int {
		if a < b {
			return -1
		} else if a > b {
			return 1
		}
		return 0
	})

	fmt.Println(newList) // Prints [2,3,4]
}

Please check the documentation for more comprehensive usage.

Contributing

Any contributions to this repo are always welcome. Please check the Contribution Guidelines for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Basic collection with Go using generics

Topics

Resources

License

Stars

Watchers

Forks

Contributors 6

Languages