# gotypes **Repository Path**: opsfast/gotypes ## Basic Information - **Project Name**: gotypes - **Description**: The implementation of data type, such as simple linked list, queue, stack, etc. Uses the idea of lambda in the implementation - **Primary Language**: Go - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2020-03-16 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 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. 1. BufferedChannel : Buffered IO channel 2. HashMap : A HashMap based on Go's map 3. ArrayList : A safe ArrayList 4. LinkedList : A safe LinkedList 5. Queue : A safe FIFO Queue 6. Stack : A safe LIFO Queue 7. HashSet : A safe HashSet based on Go's map 8. FileSplitter : File Splitter by line 9. ... ## Install 1. Git ```shell script mkdir -p $GOPATH/src/gitee.com/Billcoding/gotypes cd $GOPATH/src/gitee.com/Billcoding git clone https://gitee.com/Billcoding/gotypes.git gotypes ``` 2. IDE(Goland) ```shell script import "gitee.com/Billcoding/gotypes/set" ``` ## Usage * BufferedChannel ```shell script 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)) } } ``` * HashMap ```shell script 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) }) } ``` * ArrayList ```shell script 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() } ``` * LinkedList ```shell script 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) }) } ``` * Queue ```shell script 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) }) } ``` * Stack ```shell script 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) }) } ``` * HashSet ```shell script 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) }) } ``` * FileSplitter ```shell script package main func main() { //Get new FileSplitter splitter := NewFileSplitter("text.txt", "", 30) //Start split splitter.Split() } ``` ## Docs [Wiki](https://gitee.com/Billcoding/gotypes/wikis)