Go Packages,Algorithms, and DS(Big O notation | Sort | Linked Lists | Trees|Garbage collection)

  • The Big O notation
  • Two sorting algorithms
  • The sort.Slice() function
  • Linked lists
  • Trees
  • Creating your own hash table data structure in Go
  • Go packages
  • Garbage collection(GC) in Go

About algorithms

Knowing about algorithms and the way they work will definitely help you when you have to manipulate lots of data.

raditional Unix command-line utilities such as awk(1), sed(1), vi(1), tar(1),and cp(1) are great examples of how good algorithms can help, andthese utilities can work with files that are much bigger than the memory of a machine.

The Big O notation

is used for describing the complexity of an algorithm,which is directly related to its performance. The efficiency of an algorithm is judged by its computation complexity, which mainly has to do with the number of times the algorithm needs to access its input data to do its job.

The worst-case scenario

the average situation

O(n) > O(n^{2})>O(n^{3})>O(n!)

Note that the Big O notation is more about estimating and not about giving an exact value. Therefore, it is largely used as a comparative value and not an absolute value.

Sorting algorithms

  • Quicksort(快排)             O(nlogn)
  • Bubble sort(冒泡)     O(n^2)

Although every algorithm has its disadvantages, if you do not have lots of data, the algorithm is not really important as long as it does the job.

The sort.Slice() function

[maxwell@MaxwellDBA thesortslicefunction]$ vim sortSlice.go
[maxwell@MaxwellDBA thesortslicefunction]$ go run sortSlice.go
0: [{Mihalis 180 90} {Dimitris 180 95} {Marietta 155 45} {Bill 134 40}]
<: [{Bill 134 40} {Marietta 155 45} {Mihalis 180 90} {Dimitris 180 95}]
>: [{Dimitris 180 95} {Mihalis 180 90} {Marietta 155 45} {Bill 134 40}]
[maxwell@MaxwellDBA thesortslicefunction]$ cat sortSlice.go
package main

import (
   "fmt"
   "sort"
)


type aStructure struct {
  person string
  height int
  weight int
}

func main(){


   mySlice := make([]aStructure, 0)
   a := aStructure{"Mihalis", 180, 90}
   mySlice = append(mySlice, a)
   a = aStructure{"Dimitris", 180, 95}
   mySlice = append(mySlice, a)
   a = aStructure{"Marietta", 155, 45}
   mySlice = append(mySlice, a)
   a = aStructure{"Bill", 134, 40}
   mySlice = append(mySlice, a)

   fmt.Println("0:", mySlice)
   sort.Slice(mySlice, func(i, j int) bool{return mySlice[i].weight < mySlice[j].weight})
   fmt.Println("<:",mySlice)
   sort.Slice(mySlice, func(i, j int) bool{return mySlice[i].weight > mySlice[j].weight})
   fmt.Println(">:", mySlice)
}
[maxwell@MaxwellDBA thesortslicefunction]$ 

Linked lists in Go

A linked list is a structure with a finite set of elements where each element uses at least two memory locations: one for storing the data and the other for a pointer that links the current element to the next one in the sequence of elements that make the linked list.

The biggest advantages of linked lists :

  • easy to understand and implement,
  • generic enough to be used in many different situations and model different kinds of data.

The first element of a linked list is called the head, whereas the last element of a list is often called the tail.

Note that if you lose the pointer to the first node of a single linked list, there is no possible way to find it again.

 When creating your own data structures, the single most important element is the definition of the node, which is usually implemented using a structure.

[maxwell@MaxwellDBA linkedlist]$ vim linkedList.go
[maxwell@MaxwellDBA linkedlist]$ 
[maxwell@MaxwellDBA linkedlist]$ go run linkedList.go
&{0 <nil>}
-> Empthy list!
Node already exists: 1
1 -> 
Node already exists: 0
1 -> 10 -> 5 -> 0 -> 
1 -> 10 -> 5 -> 0 -> 100 -> 
[maxwell@MaxwellDBA linkedlist]$ cat linkedList.go
package main

import (
  "fmt"
)

type Node struct {
   Value int
   Next *Node
}

func addNode(t *Node, v int) int {
   if root == nil {
         t = &Node{v, nil}
         root = t
         return 0
   }

   if v == t.Value {
         fmt.Println("Node already exists:", v)
         return -1
   }


   if t.Next == nil {
        t.Next = &Node{v, nil}
        return -2
   }


   return addNode(t.Next, v)
}

func traverse(t *Node) {
   if t == nil {
         fmt.Println("-> Empthy list!")
         return
   }

   for t != nil {
         fmt.Printf("%d -> ", t.Value)
         t = t.Next
   }
   fmt.Println()
}

var root = new(Node)
func main(){
   fmt.Println(root)
   root = nil
   traverse(root)
   addNode(root, 1)
   addNode(root, 1)
   traverse(root)
   addNode(root, 10)
   addNode(root, 5)
   addNode(root, 0)
   addNode(root, 0)
   traverse(root)
   addNode(root, 100)
   traverse(root)
}
[maxwell@MaxwellDBA linkedlist]$ 

Trees in Go

A graph is a finite and nonempty set of vertices and edges. A directed graph is a graph whose edges have a direction associated with them. A directed acyclic graph is a directed graph with no cycles in it. A tree is a directed acyclic graph that satisfies three more principles: firstly, it has a root node: the entry point to the tree; secondly, every vertex, except the root, has one and only one entry point; and thirdly, there is a path that connects the root with each vertex and belongs to the tree.

The most commonly used type of tree is called a binary tree because each node can have up to two children. The following figure shows a graphical representation of a binary tree's data structure:

[maxwell@MaxwellDBA trees]$ vim tree.go
[maxwell@MaxwellDBA trees]$ go run tree.go
0 2 3 2 4 0 2 3 2 5 0 2 3 2 4 0 2 3 2 6 0 2 3 2 7 8 2 7 9 7 10 7 9 7 12 0 2 3 2 4 0 2 3 2 6 0 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 19 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 20 16 21 7 9 7 10 7 9 7 14 7 9 7 16 20 16 22 0 2 3 2 6 0 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 19 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 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9 7 14 7 9 7 16 20 16 24 25 7 9 7 14 7 9 7 16 20 16 24 26 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 20 16 21 7 9 7 10 7 9 7 14 7 9 7 16 20 16 24 25 7 9 7 14 7 9 7 16 20 16 24 27 16 20 16 24 28 0 2 3 2 4 0 2 3 2 5 0 2 3 2 4 0 2 3 2 6 0 2 3 2 7 8 2 7 9 7 10 7 9 7 12 0 2 3 2 4 0 2 3 2 6 0 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 19 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 20 16 21 7 9 7 10 7 9 7 14 7 9 7 16 20 16 22 0 2 3 2 6 0 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 19 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 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2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 20 16 21 7 9 7 10 7 9 7 14 7 9 7 16 20 16 24 25 7 9 7 14 7 9 7 16 20 16 24 26 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 15 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 17 2 3 2 7 8 2 7 9 7 10 7 9 7 13 2 7 8 2 7 9 7 10 7 9 7 14 7 9 7 16 20 16 21 7 9 7 10 7 9 7 14 7 9 7 16 20 16 24 25 7 9 7 14 7 9 7 16 20 16 24 27 16 20 16 24 29 2 7 9 7 10 7 9 7 14 7 9 7 16 20 16 21 7 9 7 10 7 9 7 14 7 9 7 16 20 16 24 25 7 9 7 14 7 9 7 16 20 16 24 27 16 20 16 24 
The value of the root of the tree is 28
[maxwell@MaxwellDBA trees]$ cat tree.go
package main

import (
   "fmt"
   "math/rand"
   "time"
)

type Tree struct {
  Left *Tree
  Value int
  Right *Tree
}

func traverse(t *Tree){
   if t == nil {
         return
    }
    traverse(t.Left)
    fmt.Print(t.Value, " ")
    traverse(t.Right)
}

func create(n int) * Tree {
   var t *Tree
   rand.Seed(time.Now().Unix())
   for i := 0; i< 2*n; i++ {
         temp := rand.Intn(n)
         t = insert(t, temp)
   }
   return t
}

func insert(t *Tree, v int) *Tree {
   if t == nil {
         return&Tree{nil, v, nil}
   }
   if v== t.Value {
        return t
   }
   if v < t.Value {
          t.Left = insert(t.Left, v)
   }
   t.Right = insert(t.Right, v)
   return t
}

func main() {
   tree := create(30)
   traverse(tree)
   fmt.Println()
   fmt.Println("The value of the root of the tree is", tree.Value)
}
[maxwell@MaxwellDBA trees]$

 Please note that as the values of the nodes of the tree are generated randomly, the output of the program will be different each time you run it. If you want to get the same elements all the time, then use a constant for the seed value in the create() function.

Developing a hash table in Go

a hash table is a data structure that stores one or more key and value pairs and uses the hashFunction of the key to compute an index into an array of buckets or slots, from which the correct value can be retrieved.Ideally, the hashFunction should assign each key to a unique bucket, provided that you have the required number of buckets.

The following figure shows the graphical representation of a simple hash table with 10 buckets. It is not difficult to understand that the hashFunction is the modulo operator:

 The source code of hash.go will be presented as below:

[maxwell@MaxwellDBA hashtable]$ vim hash.go
[maxwell@MaxwellDBA hashtable]$ cat hash.go   
package main

import (
   "fmt"
)

type Node struct {
  Value int
  Next *Node
}


type HashTablestruct {
   Table map[int]*Node
   Size int
}

func hashFunction(i, size int) int {
   return (i % size)
}

func insert(hash *HashTable, value int) int {
   index := hashFunction(value, hash.Size)
   element := Node{Value: value, Next: hash.Table[index]}
   hash.Table[index] = &element
   return index

}

func traverse(hash *HashTable) {
   for k := range hash.Table {
         if hash.Table[k] != nil {
               t := hash.Table[k]
               for t != nil {
                     fmt.Printf("%d ->", t.Value)
                     t = t.Next
               }
               fmt.Println()
        }
   }
}

func main() {
   table := make(map[int]*Node, 10)
   hash := &HashTable{Table: table, Size: 10}
   fmt.Println("Number of spaces:", hash.Size)
   for i :=  0; i < 95; i++ {
         insert(hash, i)
   }

   traverse(hash)
}
[maxwell@MaxwellDBA hashtable]$ 

About Go packages

Packages are for grouping related functions and constants.

There exist many useful Go packages that come with each Go distribution including the following:

  • The net package:This supports portable TCP and UDP connections.
  • The http package: This is a part of the net package and offers HTTP server and client implementations
  • The match package: This provides mathematical functions and constants
  • The io package: This deals with primitive input and output operations.
  • The os package: This gives you a portable interface to the operating system functionality
  • The time package: This allows you to work with times and dates.

Go packages refer to https://golang.org/pkg/

Using standard Go packages

the net package has several sub directories,named http, mail, rpc, smtp, textproto, and url, which should be imported as net/http, net/mail, net/rpc, net/smtp, net/textproto, and net/url,respectively.

You can find information about a Go standard package with the help of the godoc utility. So, if you are looking for information about the net package, you should execute godoc net.

Garbage collection

[maxwell@MaxwellDBA garbagecollection]$ vim garbageCol.go   
[maxwell@MaxwellDBA garbagecollection]$ go run garbageCol.go
mem.Alloc: 130376
mem.TotalAlloc: 130376
mem.HeapAlloc: 130376
mem.NumGC 0
---------
mem.Alloc: 200151600
mem.TotalAlloc: 1000215984
mem.HeapAlloc: 200151600
mem.NumGC 10
---------
mem.Alloc: 300164992
mem.TotalAlloc: 2000301416
mem.HeapAlloc: 300164992
mem.NumGC 16
---------
[maxwell@MaxwellDBA garbagecollection]$ cat garbageCol.go
package main

import (
   "fmt"
   "runtime"
   "time"
)

func printStats(mem runtime.MemStats){
   runtime.ReadMemStats(&mem)
   fmt.Println("mem.Alloc:", mem.Alloc)
   fmt.Println("mem.TotalAlloc:", mem.TotalAlloc)
   fmt.Println("mem.HeapAlloc:", mem.HeapAlloc)
   fmt.Println("mem.NumGC", mem.NumGC)
   fmt.Println("---------")
}


func main(){
   var mem runtime.MemStats
   printStats(mem)

   for i := 0; i < 10; i++ {
         s := make([]byte, 100000000)
         if s == nil {
               fmt.Println("Operation failed!")
        }
   }
   printStats(mem)

   for i := 0; i < 10; i++ {
         s := make([]byte, 100000000)
         if s == nil {
               fmt.Println("Operation failed!")
        }
        time.Sleep(5 * time.Second)
   }
   printStats(mem)
}

[maxwell@MaxwellDBA garbagecollection]$ 

Every time you want to read the latest memory statistics, you should make a call to the runtime.ReadMemStats() function.

the output presents information about properties related to the memory used by the garbageCol.go program. If you want to get an even more detailed output,you can execute garbageCol.go, as shown here:

[maxwell@MaxwellDBA garbagecollection]$ 
[maxwell@MaxwellDBA garbagecollection]$ GODEBUG=gctrace=1 go run garbageCol.go
mem.Alloc: 130408
mem.TotalAlloc: 130408
mem.HeapAlloc: 130408
mem.NumGC 0
---------
gc 1 @0.050s 0%: 0.058+0.23+0.065 ms clock, 0.11+0.19/0/0.16+0.13 ms cpu, 95->95->95 MB, 96 MB goal, 2 P
gc 2 @0.053s 0%: 0.044+0.33+0.088 ms clock, 0.089+0.14/0.013/0.15+0.17 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 3 @0.137s 0%: 0.074+0.26+0.053 ms clock, 0.14+0.14/0.078/0.11+0.10 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 4 @0.221s 0%: 0.046+0.35+0.086 ms clock, 0.092+0.18/0.12/0.14+0.17 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 5 @0.268s 0%: 0.038+0.28+0.077 ms clock, 0.077+0.093/0.15/0.096+0.15 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 6 @0.314s 0%: 0.038+0.23+0.062 ms clock, 0.076+0.15/0.065/0.095+0.12 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 7 @0.360s 0%: 0.055+0.21+0.055 ms clock, 0.11+0.16/0.064/0.10+0.11 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 8 @0.406s 0%: 0.054+0.25+0.086 ms clock, 0.10+0.16/0.070/0.096+0.17 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 9 @0.453s 0%: 0.068+0.29+0.093 ms clock, 0.13+0.18/0.14/0.11+0.18 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
gc 10 @0.538s 0%: 0.046+0.27+0.048 ms clock, 0.093+0/0.14/0.18+0.096 ms cpu, 190->190->95 MB, 191 MB goal, 2 P
mem.Alloc: 200151632
mem.TotalAlloc: 1000216400
mem.HeapAlloc: 200151632
mem.NumGC 10
---------
gc 11 @0.584s 0%: 0.041+0.24+0.096 ms clock, 0.082+0.17/0.048/0.096+0.19 ms cpu, 190->190->190 MB, 191 MB goal, 2 P
gc 12 @5.631s 0%: 0.037+0.51+0.056 ms clock, 0.075+0/0.088/0.25+0.11 ms cpu, 286->286->190 MB, 381 MB goal, 2 P
gc 13 @10.677s 0%: 0.033+0.43+0.027 ms clock, 0.067+0/0.15/0.20+0.054 ms cpu, 286->286->190 MB, 381 MB goal, 2 P
gc 14 @20.809s 0%: 0.042+0.28+0.066 ms clock, 0.085+0/0.083/0.24+0.13 ms cpu, 381->381->190 MB, 382 MB goal, 2 P
gc 15 @30.901s 0%: 0.056+0.36+0.055 ms clock, 0.11+0/0.18/0.13+0.11 ms cpu, 381->381->190 MB, 382 MB goal, 2 P
gc 16 @40.994s 0%: 0.037+0.39+0.051 ms clock, 0.074+0/0.10/0.21+0.10 ms cpu, 381->381->190 MB, 382 MB goal, 2 P
mem.Alloc: 300165056
mem.TotalAlloc: 2000302184
mem.HeapAlloc: 300165056
mem.NumGC 16
---------
[maxwell@MaxwellDBA garbagecollection]$ 

Your environment

We will show how to find out things about your environment using the runtime package : this can be useful when you have to take certain actions depending on the OS and the Go version you are using.

The use of the runtime package for finding out about your environments is straightforward and is illustrated in runTime.go:

[maxwell@MaxwellDBA runTime]$ vim runTime.go
[maxwell@MaxwellDBA runTime]$ go run runTime.go
You are using gccgo on a amd64 machine
with Go version go1.10.3 gccgo (GCC) 8.5.0 20210514 (Red Hat 8.5.0-15)
Number of Goroutines: 1
[maxwell@MaxwellDBA runTime]$ cat runTime.go
package main

import (
   "fmt"
   "runtime"
)

func main() {
   fmt.Print("You are using ", runtime.Compiler, " ")
   fmt.Println("on a", runtime.GOARCH, "machine")
   fmt.Println("with Go version", runtime.Version())
   fmt.Println("Number of Goroutines:", runtime.NumGoroutine())
}
[maxwell@MaxwellDBA runTime]$ 

Go gets updated frequently!

[maxwell@MaxwellDBA runTime]$ 
[maxwell@MaxwellDBA runTime]$ date
Tue Dec 13 17:02:37 CST 2022
[maxwell@MaxwellDBA runTime]$ go version
go version go1.10.3 gccgo (GCC) 8.5.0 20210514 (Red Hat 8.5.0-15) linux/amd64
[maxwell@MaxwellDBA runTime]$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值