मैं जाओ टूर exercise #71golang: जब तक मैं एक fmt.Print()
की कोशिश की, यह ठीक काम करता है go run 71_hang.go ok
की तरह चलाया जाता है तो जोड़ा चयन के साथ goroute नहीं रूकती है।
हालांकि, यदि आप go run 71_hang.go nogood
का उपयोग करते हैं, तो यह हमेशा के लिए चलाएगा।
select
कथन में default
में अतिरिक्त fmt.Print("")
अतिरिक्त अंतर है।
मुझे यकीन नहीं है, लेकिन मुझे किसी प्रकार की अनंत लूप और रेस-हालत पर संदेह है? और यहाँ मेरा समाधान है।
ध्यान दें: यह गतिरोध नहीं कर रहा है के रूप में जाओ throw: all goroutines are asleep - deadlock!
package main
import (
"fmt"
"os"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)
}
func crawl(todo Todo, fetcher Fetcher,
todoList chan Todo, done chan bool) {
body, urls, err := fetcher.Fetch(todo.url)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("found: %s %q\n", todo.url, body)
for _, u := range urls {
todoList <- Todo{u, todo.depth - 1}
}
}
done <- true
return
}
type Todo struct {
url string
depth int
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher) {
visited := make(map[string]bool)
doneCrawling := make(chan bool, 100)
toDoList := make(chan Todo, 100)
toDoList <- Todo{url, depth}
crawling := 0
for {
select {
case todo := <-toDoList:
if todo.depth > 0 && !visited[todo.url] {
crawling++
visited[todo.url] = true
go crawl(todo, fetcher, toDoList, doneCrawling)
}
case <-doneCrawling:
crawling--
default:
if os.Args[1]=="ok" { // *
fmt.Print("")
}
if crawling == 0 {
goto END
}
}
}
END:
return
}
func main() {
Crawl("http://golang.org/", 4, fetcher)
}
// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult
type fakeResult struct {
body string
urls []string
}
func (f *fakeFetcher) Fetch(url string) (string, []string, error) {
if res, ok := (*f)[url]; ok {
return res.body, res.urls, nil
}
return "", nil, fmt.Errorf("not found: %s", url)
}
// fetcher is a populated fakeFetcher.
var fetcher = &fakeFetcher{
"http://golang.org/": &fakeResult{
"The Go Programming Language",
[]string{
"http://golang.org/pkg/",
"http://golang.org/cmd/",
},
},
"http://golang.org/pkg/": &fakeResult{
"Packages",
[]string{
"http://golang.org/",
"http://golang.org/cmd/",
"http://golang.org/pkg/fmt/",
"http://golang.org/pkg/os/",
},
},
"http://golang.org/pkg/fmt/": &fakeResult{
"Package fmt",
[]string{
"http://golang.org/",
"http://golang.org/pkg/",
},
},
"http://golang.org/pkg/os/": &fakeResult{
"Package os",
[]string{
"http://golang.org/",
"http://golang.org/pkg/",
},
},
}
चयन उपज नहीं है * क्योंकि डिफ़ॉल्ट बयान के * ।हालांकि मुझे यकीन नहीं है कि अगर आप पूरी तरह से समझ नहीं पाते हैं, क्योंकि आपने "डिफ़ॉल्ट" और GOMAXPROCS स्पष्टीकरणों को खींचा है। – mna
यही वही है जो मुझे नहीं पता था, धन्यवाद! –
"डिफ़ॉल्ट कथन के कारण चयन नहीं करता है।" वह है जो मुझे नहीं पता। धन्यवाद। – Sungam