Last active
March 10, 2021 02:14
-
-
Save uloureiro/c3fd0350308f62217ca1d7ac96ff90d5 to your computer and use it in GitHub Desktop.
Slices and maps and concurrency in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://play.golang.org/p/L8MVDTGHqhi | |
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
feedIP([]string{"255.255.255.0", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "255.255.255.0", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1", "255.255.255.0"}) | |
//feedIP([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"}) | |
} | |
func feedIP(input []string) { | |
var mu sync.Mutex | |
var wg sync.WaitGroup | |
ips := make(map[string]int) | |
size := len(input) / 4 // cosidering 4 cores processor, 1 thread per core | |
slInput1 := input[:size] // from index 0 to position ´size´ (index ´size - 1´), in a slice of 1..100, corresponds to 1 to 25 | |
slInput2 := input[size : size*2] // from index ´size´ to position ´size*2´ (index ´size*2-1´), in a slice of 1..100 corresponds to 26 to 50 | |
slInput3 := input[size*2 : size*3+1] // from index ´size*2´ to position ´size*3´ (index ´size*3-1´), in a slice of 1..100 corresponds to 51 to 76 | |
slInput4 := input[size*3+1:] // from index ´size*3 + 1´ to the end, in a slice of 1..100, corresponds to 77 to 100; this approach (instead of using len(input)) is needed to handle properly the offset in slices with odd sizes | |
/* debug purposes | |
fmt.Println(size) | |
fmt.Println(slInput1) | |
fmt.Println(slInput2) | |
fmt.Println(slInput3) | |
fmt.Println(slInput4) | |
*/ | |
// ´values´ is passed by value, ´bucket´ is passed by ref (not passed but map elements are pointers by design), ´wg´ is explicitly passed by ref | |
feeder := func(values []string, bucket map[string]int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for _, i := range values { | |
func() { | |
mu.Lock() | |
defer mu.Unlock() | |
bucket[i]++ | |
}() | |
} | |
} | |
wg.Add(1) | |
go feeder(slInput1, ips, &wg) | |
wg.Add(1) | |
go feeder(slInput2, ips, &wg) | |
wg.Add(1) | |
go feeder(slInput3, ips, &wg) | |
wg.Add(1) | |
go feeder(slInput4, ips, &wg) | |
wg.Wait() | |
fmt.Println(ips) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment