collection

package module
v0.0.0-...-d73a1c8 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: MIT Imports: 1 Imported by: 0

README

GoCollection

Simple key:value based utility data structure for golang!

Quick Example

First install the package!

go get github.com/scientific-dev/gocollection

In your golang file!

package main

import "fmt"
import collection "github.com/scientific-dev/gocollection"

func main() {
    col := collection.Collection()
    col.Set("foo", "bar")
    fmt.Println(col.Get("foo")) // Will return "bar"
}

It is basically a better version of map in golang! GoCollection uses map[string]interface{} as a map typing!

Storing structs

So incase if you are not aware how to store structs using this, here is quick tuto

package main

import "fmt"
import collection "github.com/scientific-dev/gocollection"

type SimpleStruct struct{
    field string
}

func main() {
    col := collection.Collection()
    col.Set("foo", SimpleStruct{ field: "string" })
    
    fmt.Println(col.Get("foo").(SimpleStruct).field) // You can use the basic type conversion of golang for this!
}

Support

Any kind of doubts on this package, you can make an issue in the github repo or join our discord server and ask us doubts!

Discord Server: https://discord.gg/FrduEZd
GitHub Repo: https://github.com/scientific-dev/gocollection/
Docs: https://github.com/scientific-dev/gocollection/wiki/Go-Collection
Golang: https://pkg.go.dev/github.com/scientific-dev/gocollection

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CollectionConstructor

type CollectionConstructor struct {
	// contains filtered or unexported fields
}

Structure for the collection constructor!

func Collection

func Collection() CollectionConstructor

The main collection function which returns the collection constructor!

func (CollectionConstructor) All

func (self CollectionConstructor) All() []Dataset

Returns an array of datasets containing all the registered keys and values!

func (CollectionConstructor) Concat

func (self CollectionConstructor) Concat(collections ...CollectionConstructor)

Adds all the keys and values of other collections too!

func (CollectionConstructor) Delete

func (self CollectionConstructor) Delete(key string)

Deletes the value by the key

func (CollectionConstructor) DeleteAll

func (self CollectionConstructor) DeleteAll()

Deletes everything what is registered!

func (CollectionConstructor) Exists

func (self CollectionConstructor) Exists(key string) bool

Returns a boolean stating the key is registered or not!

func (CollectionConstructor) Filter

func (self CollectionConstructor) Filter(callback func(value interface{}, key string) bool) CollectionConstructor

Runs the callback on every registered key and value. Sends a new collection with the datasets which have passed the callback!

func (CollectionConstructor) Find

func (self CollectionConstructor) Find(callback func(value interface{}, key string) bool) (value interface{}, found bool)

Runs the callback function with each registered key and value. If any dataset passes the test will return that key and value else will return false in the found tupule!

func (CollectionConstructor) ForEach

func (self CollectionConstructor) ForEach(callback func(value interface{}, key string))

Runs the callback function with each registered key and value

func (CollectionConstructor) Get

func (self CollectionConstructor) Get(key string) (value interface{}, found bool)

Returns the value of the key but in tupule in the form of (value, found). Found will be a boolean stating is the value found or not. Value will be nil if no id has been registered already!

func (CollectionConstructor) Keys

func (self CollectionConstructor) Keys() []string

Returns an array of strings containing all registered keys!

func (CollectionConstructor) Length

func (self CollectionConstructor) Length() int

Returns the number of keys registered!

func (CollectionConstructor) Map

func (self CollectionConstructor) Map(callback func(value interface{}, key string) interface{}) CollectionConstructor

Runs the callback on every registered key and value. Replaces the values of each key with the value returned by the callback!

func (CollectionConstructor) Random

func (self CollectionConstructor) Random() Dataset

Returns a random dataset from CollectionConstructor.All()! If not even 1 key is registered will return "Dataset{ "nil", nil }"

func (CollectionConstructor) Set

func (self CollectionConstructor) Set(key string, value interface{})

Sets a value for the key!

func (CollectionConstructor) Sweep

func (self CollectionConstructor) Sweep(callback func(value interface{}, key string) bool)

Runs the callback on every registered key and value. If it fails the callback, will delete it from the cache!

func (CollectionConstructor) Values

func (self CollectionConstructor) Values() []interface{}

Returns an array of registered values!

type Dataset

type Dataset struct {
	// contains filtered or unexported fields
}

Structure for the basic Dataset!

Jump to

Keyboard shortcuts

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