In Go programming, the "runtime error: invalid memory address or nil pointer dereference" often occurs when accessing or dereferencing memory that is not valid. This error may arise in various contexts, including network operations like HTTP requests.
Regarding your webcrawler, the error is likely related to accessing the response body (resp.Body) without first checking for errors. In the basicFetcher function, for example, if http.Get returns an error, the resp variable will be nil, and subsequent attempts to access resp.Body will fail due to nil pointer dereferencing.
To address this issue, it's crucial to handle errors appropriately. Consider modifying your basicFetcher function as follows:
func basicFetcher(uri string, c chan int) { resp, err := http.Get(uri) if err != nil { fmt.Println(err) return } defer resp.Body.Close() _, err = ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } c <- resp.StatusCode }
Now, if http.Get or ioutil.ReadAll encounters an error, the function will return gracefully, preventing the nil pointer dereference error.
As suggested by twotwotwo, it's good practice to return a (result, error) pair from your functions. This allows you to handle errors and terminate the function if necessary. By returning an error, you can provide more context for the error and enable better error handling in your main function.
The above is the detailed content of How Can I Prevent 'runtime error: invalid memory address or nil pointer dereference' in My Go Web Crawler?. For more information, please follow other related articles on the PHP Chinese website!