Home > Backend Development > Golang > How Can I Retrieve and Store Webpage Content as a String in Go?

How Can I Retrieve and Store Webpage Content as a String in Go?

Mary-Kate Olsen
Release: 2024-12-02 10:33:11
Original
632 people have browsed it

How Can I Retrieve and Store Webpage Content as a String in Go?

Retrieving Webpage Content as a String in Go

In Go, the http package provides a straightforward way to interact with web pages. This includes the ability to fetch the content of a webpage and store it as a string, allowing you to process the data in various ways.

Getting Started with the http Package

To retrieve webpage content in Go, you need to establish an HTTP connection using the http.Get function. The function takes a URL as an input argument and returns an HTTP response.

res, err := http.Get("https://example.com")
Copy after login

If the HTTP request is successful, you can access the response's body using res.Body. To extract the webpage content as a string, use the io.ReadAll function:

content, err := io.ReadAll(res.Body)
Copy after login

Remember to close the response body after reading the content to release the associated resources.

res.Body.Close()
Copy after login

Example Implementation

Here's an example function that takes a URL as input and returns the webpage content as a string:

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)
}
Copy after login

Example Usage

To use this function, you can call it with the URL of the webpage you want to retrieve:

fmt.Println(OnPage("http://www.bbc.co.uk"))
Copy after login

This code would print the string representation of the BBC website's content to the console. You can then process this string using any of Go's string manipulation capabilities.

The above is the detailed content of How Can I Retrieve and Store Webpage Content as a String in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template