Skip to content

Instantly share code, notes, and snippets.

@NSEcho
Created May 31, 2021 13:08
Show Gist options
  • Save NSEcho/5802c610fb4ce7420bd3c82b31325975 to your computer and use it in GitHub Desktop.
Save NSEcho/5802c610fb4ce7420bd3c82b31325975 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
type checkFn func(username string) bool
type SingleCheck struct {
URL string
checkFn checkFn
}
func (s *SingleCheck) Check(username string, wg *sync.WaitGroup, results chan<- Result) {
defer wg.Done()
res := Result{
URL: s.URL,
Function: s.checkFn,
Found: false,
}
if s.checkFn(username) {
res.Found = true
}
results <- res
}
type Result struct {
URL string
Found bool
Function checkFn
}
type Checker struct {
Username string
Results chan Result
Checks []SingleCheck
}
func (c *Checker) Run() {
done := make(chan struct{})
go func() {
for res := range c.Results {
fmt.Println(res)
}
done <- struct{}{}
}()
var wg sync.WaitGroup
wg.Add(len(c.Checks))
for _, check := range c.Checks {
check.Check(c.Username, &wg, c.Results)
}
wg.Wait()
close(c.Results)
<-done
}
func main() {
checker := &Checker{
Username: "Erhad",
Results: make(chan Result, 50),
Checks: []SingleCheck{
SingleCheck{
URL: "https://www.google.com",
checkFn: checkStatusCode,
},
SingleCheck{
URL: "https://www.facebook.com",
checkFn: checkRegex,
},
SingleCheck{
URL: "https://www.klix.ba",
checkFn: checkStatusCode,
},
},
}
checker.Run()
}
func checkStatusCode(username string) bool {
return true
}
func checkRegex(username string) bool {
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment