Professional Documents
Culture Documents
ACP Lecture 05 - Maps and Packages
ACP Lecture 05 - Maps and Packages
Lecture – 05
Maps and Packages
• In maps, the values are not unique like keys and can be of any type like int,
float64, rune, string, pointer, reference type, map type, etc.
• The type of keys and type of values must be of the same type, different types of
keys and values in the same maps are not allowed. But the type of key and the
type values can differ.
• The map is also known as a hash map, hash table, unordered map, dictionary, or
associative array.
• In maps, you can only add value when the map is initialized if you try to add
value in the uninitialized map, then the compiler will throw an error.
Important Questions
Simple: In this method, you can create and initialize a map without the use
of make() function:
Creating Map: You can simply create a map using the given syntax:
// An Empty map
map[Key_Type]Value_Type{}
map_2 := map[int]string{
90: “Animal",
91: “Birds",
92: “Pets",
}
fmt.Println("Map-2: ", map_2)
}
Continue…
As usual in Go, we can declare our variable first, and then assign it a value:
Alternatively, we can use the short declaration syntax to do both operations in one step:
menu := map[string]float64{
"eggs": 1.75,
"bacon": 3.22,
"sausage": 1.89,
}
Existence of key value item
func main() {
// Creating struct instances
a2 := Address{Name: “Mujtaba", city: “Lahore", Pincode: 5600}
a1 := Address{“Hassan", “Okara", 5400}
a3 := Address{Name: “Ahmad", city: “Islamabad", Pincode: 2700}
// Declaring a map
var mp map[Address]int
// Checking if the map is empty or not
if mp == nil {
fmt.Println("True")
} else {
fmt.Println("False")
}
// Declaring and initialising
// using map literals
sample := map[Address]int{a1: 1, a2: 2, a3: 3}
fmt.Println(sample)
}
Golang Slices
It is possible to create a slice of map data type in Golang as well. In fact, a slice
can be created of any data type in Go. Below is a simple example to create a slice
of map
package main
import "fmt"
func main() {
maps := make([]map[string]string, 3)
map1 := make(map[string]string)
map1["1"] = "a"
map2 := make(map[string]string)
map2["2"] = "b"
map3 := make(map[string]string)
map3["3"] = "c"
maps[0] = map1
maps[1] = map2
maps[2] = map3
for _, m := range maps {
fmt.Println(m)
}
}
Continue…
map1 := make(map[string]string)
map1["1"] = "a"
map2 := make(map[string]string)
map2["2"] = "b"
map3 := make(map[string]string)
map3["3"] = "c"
Go Standard Libraries
Continue…
Continue…
Continue…
Continue…
package main
import "fmt"
func main() {
var intSlice = []int{10, 20, 30, 40}
fmt.Println(intSlice[0])
fmt.Println(intSlice[1])
fmt.Println(intSlice[0:3])
}
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Continue…
Source: https://pkg.go.dev/std
RegEx in GoLang
A regular expression is a sequence of characters that is used for searching and
replacing text and pattern matching.
Note: This expression is not the property of just one particular programming
language; instead, it is supported by multiple languages.
Continue…
Regular expression patterns
There are certain expressions that need to be used to search and match patterns;
some of these are given in the table below.
Expression Description
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile("ck$")
fmt.Println(re.FindString("hack"))
}
Golang Sync package
wg.Done()
}()
}
wg.Wait()
fmt.Println("ops",opt)
}
Mutex
Mutex, mutual exclusion lock is a one way of accessing shared block of code
safely by multiple goroutines. Using mutex programmer can use traditional
locking way to handle concurrency issues like race conditions.
func mutexDemo(){
var state = make(map[int]int)
var mutex = &sync.Mutex{}
time.Sleep(time.Second)
readOpsFinal:=atomic.LoadUint64(&readOps)
fmt.Println("readOps:",readOpsFinal)
writeOpsFinal:=atomic.LoadUint64(&writeOps)
fmt.Println("writeOps",writeOpsFinal)
fmt.Println("state",state)
}
RWmutex
This is an extension of mutex which introduce another two methods RLock and
RUnlock. This can be used to get more flexibility between read lock and write
lock. There are few important.
RLock is a shared read lock, so multiple threads can call RLock that means
multiple threads can reads at same time
If the mutex is read locked, a call to Lock is blocked. Which means if at least one
read lock is being called unable to write
If the mutex is write locked(with Lock) RLock will block.
If the reader counter is>0 and Lock is called, further calls to RLock will also block
until existing readers have released their locks, the writer has obtained his lock and
later release it
In a situation such that more number of reads occurring and less number of writing
it’s a good idea to use RWmutex instead of normal mutex.
Continue…
type mapLock struct {
lock sync.RWMutex
m map[int]string
}
var myMapLock = mapLock{
m:make(map[int]string),
}
func lockWithMap() {
go func() {
for i:=0;i<50;i++ {
writeLockWithMap(1)
}
}()
Continue…