list

package
v2.0.0-...-4b7107c Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 3 Imported by: 0

README

SmartList


Description

An arbitrary type list struct with methods to make it easier to use.

Usage:

Important
  1. You can Add() any type of data supported by any, but...
  2. Once you add the first record, every record afterward must be of the same type.
  3. Only if you empty the SmartList of all records can you change type.
Declaration
var list SmartList
Multi-type Support
var listBool SmartList
list.Add(true)
list.Add(true)
list.Add(false)
list.Add(true)

var listInt SmartList
list.Add(1)
list.Add(5)
list.Add(8)
list.Add(1)
Add Records: .Add()

The .Add() method will typecheck your input and if ok, append it to the list of records in the list:

list.Add("foo")  // Add record to list (and set the list to be strings only.
list.Add("bar")
list.Add("moo")
list.Add("bar")

But if you attempt to add a value that is not the same type as list[0] an error will be raised.

list.Add(true) // will return `exit.ErrTypeMismatch` because true is bool
               // and the list contains strings.
TypeCheck Methods: .TypeCheck()

The .Add() method uses .TypeCheck() to verify that a new record is of the same type as list[0]. If this is not the case, an exit.ErrTypeMismatch error is returned.

list.TypeCheck(1) // will return `exit.ErrTypeMismatch` because 1 is int 
                  // and the list contains strings.
String Method: .String()

The contents of SmartList can be returned as []string using the .String() method.

list.String() //Returns []string{"foo","bar","moo","mar"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SmartList

type SmartList []any

SmartList - A list with methods and some more advanced features.

func (*SmartList) Add

func (list *SmartList) Add(record any) (err error)

Add - Add a record to the list (list must be empty or match type of list[0])

func (*SmartList) String

func (list *SmartList) String() []string

String - Dump the contents of our SmartList to a list of strings.

func (*SmartList) TypeCheck

func (list *SmartList) TypeCheck(data any) (err error)

TypeCheck - verify that the given data is of the same type as list[0]

Jump to

Keyboard shortcuts

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