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

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-06 06:29:15780browse

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

Webpage Content Acquisition in Go: A Comprehensive Guide

Getting the content of a webpage into a string is a fundamental task for web data processing in Go. If you are new to the language, understanding the process can be challenging. This article will guide you through the steps of writing a function, OnPage, that takes a URL as an argument and returns the webpage content as a string.

Preparing with the HTTP Package

The Go standard library provides an http package to handle HTTP requests and responses. To begin, import this package into your code:

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

Crafting the OnPage Function

The goal of the OnPage function is to retrieve the webpage content as a string. Here's how to structure the function:

func OnPage(link string) (string) {
    // HTTP request to retrieve the webpage
    res, err := http.Get(link)
    if err != nil {
        log.Fatal(err)
    }
    
    // Reading the response body into a slice of bytes
    content, err := io.ReadAll(res.Body)
    res.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    
    // Converting the bytes to a string
    return string(content)
}

Making a Test Request

In the main function, you can call OnPage with a URL to demonstrate its functionality:

func main() {
    fmt.Println(OnPage("http://www.bbc.co.uk/news/uk-england-38003934"))
}

Running this program will print the webpage content of the BBC News UK England page as a string. This string can then be processed and manipulated as needed.

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

Statement:
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