Loading Webpage Content into a String in Go
In Go, the net/http package offers a convenient way to retrieve and work with webpage content. To capture the content of a webpage as a string, follow these steps:
For example, consider the following function:
func OnPage(link string) string { res, err := http.Get(link) if err != nil { log.Fatal(err) } content, err := io.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) } return string(content) }
This function takes a website URL as a parameter and returns the content of the webpage as a string. You can use this function to obtain and process the content from different web pages as needed.
The above is the detailed content of How Can I Load a Webpage\'s Content into a Go String?. For more information, please follow other related articles on the PHP Chinese website!