Александър обнови решението на 11.12.2014 16:57 (преди над 3 години)
+package main
+
+import (
+ "time"
+ "fmt"
+ "net/http"
+ "io/ioutil"
+)
+
+var err = fmt.Errorf("ERROR")
+
+func SeekAndDestroy(callback func(string) bool, chunkedUrlsToCheck <-chan []string, workersCount int) (string, error) {
+ var result string
+
+ if chunkedUrlsToCheck == nil || workersCount <= 0 {
+ return result, err
+ }
+
+ for {
+ select {
+ case urls := <-chunkedUrlsToCheck:
+ go func(res *string) string {
+ check(callback, urls, res, workersCount - 1)
+ return *res
+ }(&result)
+ case <- time.After(15 * time.Second):
+ return result, err
+ }
+ }
+ return result, nil
+}
+
+func check(callback func(string) bool, urls []string, result *string, count int) bool {
+ if count <= 0 {
+ return false
+ }
+ for _, url := range urls {
+ response, err := http.Get(url)
+ if err != nil {
+ fmt.Printf("%s", err)
+ return false
+ /*} else if response.Status == false {
+ fmt.Printf("invalid url\n")*/
+ } else {
+ defer response.Body.Close()
+ contents, err := ioutil.ReadAll(response.Body)
+ if err != nil {
+ fmt.Printf("%s", err)
+ return false
+ }
+ str := CToGoString(contents[:])
+ if callback(str) {
+ *result = url
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func CToGoString(c []byte) string {
+ n := -1
+ for i, b := range c {
+ if b == 0 {
+ break
+ }
+ n = i
+ }
+ return string(c[:n+1])
+}