J'ai suivi ce solution. Il n'y a aucune condition de course détectée quand je cours le detecter de course. Mais quand je lance detecter course avec mon code, il donne l'erreur suivante:Golang Web Solution de chenille, 2 courses de données, statut de sortie 66
================== WARNING: DATA RACE Read at 0x00c42006c1e0 by goroutine 6: main.Crawl.func1() /task2.go:50 +0x53
Previous write at 0x00c42006c1e0 by main goroutine: main.Crawl() /task2.go:48 +0x692 main.main() /task2.go:66 +0x8c
Goroutine 6 (running) created at: main.Crawl() /task2.go:49 +0x61e main.main() /task2.go:66 +0x8c ================== . . . ================== WARNING: DATA RACE Read at 0x00c420094070 by goroutine 8: main.Crawl.func1() /task2.go:50 +0x53
Previous write at 0x00c420094070 by goroutine 6: main.Crawl() /task2.go:48 +0x692 main.Crawl.func1() /task2.go:51 +0x240
Goroutine 8 (running) created at: main.Crawl() /task2.go:49 +0x61e main.Crawl.func1() /task2.go:51 +0x240
Goroutine 6 (running) created at: main.Crawl() /task2.go:49 +0x61e main.main()
/task2.go:66 +0x8c
Found 2 data race(s) exit status 66
Après mon code, quelqu'un peut-il s'il vous plaît me dire où je vais mal. J'ai essayé de le comprendre depuis si longtemps mais je ne pouvais pas l'identifier.
var visited = struct {
urls map[string]bool
sync.Mutex
}{urls: make(map[string]bool)}
func Crawl(url string, depth int, fetcher Fetcher) {
if depth <= 0 {
return
}
visited.Lock()
if visited.urls[url] && visited.urls[url] == true {
fmt.Println("already fetched: ", url)
visited.Unlock()
return
}
visited.urls[url] = true
visited.Unlock()
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
}
done := make(chan bool)
for _, nestedUrl := range urls {
go func(url string, d int) {
fmt.Printf("-> Crawling child %v of %v with depth %v \n", nestedUrl, url, depth)
Crawl(url, d, fetcher)
done <- true
}(nestedUrl, depth-1)
}
for i := range urls {
fmt.Printf("<- [%v] %v/%v Waiting for child %v.\n", url, i, len(urls))
<-done
}
fmt.Printf("<- Done with %v\n", url)
}
func main() {
Crawl("http://golang.org/", 4, fetcher)
fmt.Println("Fetching stats\n--------------")
for url, err := range visited.urls {
if err != true {
fmt.Printf("%v failed: %v\n", url, err)
} else {
fmt.Printf("%v was fetched\n", url)
}
}
}
Pourriez-vous montrer le fichier complet? Peut-être sur play.google.com ou github? Maintenant, ce n'est pas évident où sont ces lignes avec la panique de course. –
C'est probablement l'utilisation de 'nestedUrl' dans l'appel' printf' de l'enfant goroutine. –
si vous ne partagez pas le fichier entier, nous ne pouvons pas savoir quelle ligne échoue –