go types
Introduction
The implementation of data type, such as simple array list, linked list, queue, stack, etc.
Uses the idea of lambda in the implementation.
- BufferedChannel : Buffered IO channel
- HashMap : A HashMap based on Go's map
- ArrayList : A safe ArrayList
- LinkedList : A safe LinkedList
- Queue : A safe FIFO Queue
- Stack : A safe LIFO Queue
- HashSet : A safe HashSet based on Go's map
- FileSplitter : File Splitter by line
- ...
Install
- Git
mkdir -p $GOPATH/src/gitee.com/Billcoding/gotypes
cd $GOPATH/src/gitee.com/Billcoding
git clone https://gitee.com/Billcoding/gotypes.git gotypes
- IDE(Goland)
import "gitee.com/Billcoding/gotypes/set"
Usage
package main
import (
"fmt"
)
func main() {
//create new buffered file channel
channel := NewBufferedChannel("text.txt", 1024)
var buffer *[]byte
//read by rune 3
buffer = channel.ReadRune('3', true)
if buffer != nil {
fmt.Println(string(*buffer))
}
//set offset 0
channel.SetOffset(0)
for {
//read line
buffer = channel.ReadLine()
if buffer == nil {
break
}
fmt.Println(string(*buffer))
}
channel.SetOffset(0)
//read all bytes
buffer = channel.ReadEnd()
if buffer != nil {
fmt.Println(string(*buffer))
}
}
package main
import (
"fmt"
)
func main() {
//Get new HashMap
hashMap := NewHashMap()
//Put an element
hashMap.Put("abc", "a")
hashMap.Put("111", "1")
hashMap.Put("222", "2")
//Check key exists
fmt.Println(hashMap.Key("abc"))
//Check Value exists
fmt.Println(hashMap.Value("abc"))
//Foreach HashMap
hashMap.ForEach(func(k Key, v Value) {
fmt.Println(k, " = ", v)
})
}
package main
import (
"fmt"
)
func main() {
//Get new ArrayList
arrayList := NewArrayList()
//Add element
arrayList.Add(10)
arrayList.Add(100)
arrayList.Add(1000)
//Get ArrayList size
fmt.Println(arrayList.Size())
//Get ArrayList [0] element
fmt.Println(arrayList.Get(0))
//Foreach function
arrayList.ForEach(func(e *gotypes.Element) {
fmt.Println(*e)
})
//Reduce function
fmt.Println("Reduce : ", arrayList.Reduce(0, func(val gotypes.Element, e *gotypes.Element) Element {
val = (*e).(int) + val.(int)
return val
}))
//Clear elements
arrayList.Clear()
}
package main
import (
"fmt"
)
func main() {
//Get new LinkedList
linkedList := NewLinkedList()
//Add element
linkedList.Add(10)
linkedList.Add(100)
linkedList.Add(1000)
linkedList.Add(10000)
//Get head element and remove it
fmt.Println(*linkedList.Take().Value)
//Get tail element and remove it
fmt.Println(*linkedList.Peek().Value)
//Get LinkedList size
fmt.Println(linkedList.Size())
//Foreach function
linkedList.ForEach(func(e *LinkedElement) {
fmt.Println(*e.Value)
})
}
package main
import (
"fmt"
)
func main() {
//Get new Queue
queue := NewQueue()
//Push element into Queue
queue.Push(777)
queue.Push(111)
queue.Push(222)
//Get element from Queue and remove it
fmt.Println(*queue.Pull())
//Foreach function
queue.ForEach(func(e *gotypes.Element) {
fmt.Println(*e)
})
}
package stack
import (
"fmt"
)
func main() {
//Get new Stack
stack := NewStack()
//Push element into Stack bottom
stack.Push(123)
stack.Push("222")
stack.Push("3333")
stack.Push("4444")
stack.Push("5555")
//Pop element from Stack
fmt.Println(*stack.Pop())
fmt.Println(*stack.Pop())
fmt.Println(*stack.Pop())
//Get Stack's size
fmt.Println(stack.Size())
//Foreach function
stack.ForEach(func(e *gotypes.Element) {
fmt.Println(*e)
})
}
package main
import (
"fmt"
)
func main() {
//Get new HashSet
hashSet := NewHashSet()
//Add element into HashSet
hashSet.Add("a")
hashSet.Add("b")
hashSet.Add("c")
//Check element exists
fmt.Println(hashSet.Contains("123"))
//Reduce function
fmt.Println("Reduce : ", hashSet.Reduce("", func(val gotypes.Element, e Element) Element {
val = (val).(string) + (e).(string)
return val
}))
//Foreach function
hashSet.ForEach(func(e Element) {
fmt.Println(e)
})
}
package main
func main() {
//Get new FileSplitter
splitter := NewFileSplitter("text.txt", "", 30)
//Start split
splitter.Split()
}
Docs
Wiki