Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ncpa0cpl/ezs
Easier Arrays and Maps for Golang
https://github.com/ncpa0cpl/ezs
Last synced: about 10 hours ago
JSON representation
Easier Arrays and Maps for Golang
- Host: GitHub
- URL: https://github.com/ncpa0cpl/ezs
- Owner: ncpa0cpl
- Created: 2023-11-27T11:40:21.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2024-08-20T12:19:18.000Z (3 months ago)
- Last Synced: 2024-08-21T13:43:06.769Z (3 months ago)
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# EZS
*Ezy Structures*Arrays and Maps that are easier to use.
## Array Example
```go
package mainimport (
. "github.com/ncpa0cpl/ezs"
)func main() {
myArray := NewArray([]string{})myArray.Push("")
myArray.Push("Hello")
myArray.Push("")
myArray.Push("World")myArray = myArray.Filter(func(value string, idx int) bool {
return value != ""
})fmt.Println(myArray.At(0)) // "hello"
fmt.Println(myArray.At(1)) // "world"
}
```## Map Example
```go
package mainimport (
. "github.com/ncpa0cpl/ezs"
)func main() {
myMap := NewMap(map[string]string{})myMap.Set("foo", "1")
myMap.Set("bar", "2")fmt.Println(myMap.Has("foo")) // true
if value, ok := myMap.Get("bar"); ok {
fmt.Println(value) // "2"
}fmt.Println(myMap.Keys()) // Array{"foo", "bar"}
fmt.Println(myMap.Values()) // Array{"1", "2"}
}
```## Iterators Example
```go
package mainimport (
. "github.com/ncpa0cpl/ezs"
)func main() {
myArray := NewArray([]string{})myArray.Push("foo")
myArray.Push("bar")
myArray.Push("baz")for value := range myArray.Iter() {
fmt.Println(value) // "foo", "bar", "baz"
}
}
```## Custom Iterators
```go
type Iterable[T any] interface {
Next() (value T, done bool)
IterReset()
}
```Any struct implementing the above interface can be used as Iterators, and be iterated over with the for range loop:
```go
package mainimport (
. "github.com/ncpa0cpl/ezs"
)type MyIterableStruct struct {
value1 string
value2 string
value3 string
iteratorNext int
}func (myStruct *MyIterableStruct) IterReset() {
myStruct.iteratorNext = 0
}func (myStruct *MyIterableStruct) Next() (string, bool) {
switch myStruct.iteratorNext {
case 0:
myStruct.iteratorNext++
return myStruct.value1, false
case 1:
myStruct.iteratorNext++
return myStruct.value2, false
case 2:
myStruct.iteratorNext++
return myStruct.value3, false
default:
return "", true
}
}func main() {
myStruct := MyIterableStruct{"value 1", "value 2", "value 3", 0}for value := range Iterator(myStruct) {
fmt.Println(value) // "value 1", "value 2", "value 3"
}
}
```