HTTP Client
To create an HTTP client program in Go, you need to create a few files: main.go
for the main logic and optionally, a client.go
file for the client functionality. Here is a step-by-step guide:
Step 1: Initialize the Go Module
First, create a new directory for your project and initialize the Go module.
mkdir httpclient
cd httpclient
go mod init github.com/username/httpclient
Step 2: Create the client.go
File
Create a client.go
file to handle the HTTP client functionality.
// client.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
// FetchURL fetches the content of the specified URL and returns it as a string.
func FetchURL(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
Step 3: Create the main.go
File
Create a main.go
file to use the client functionality.
// main.go
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: httpclient <url>")
os.Exit(1)
}
url := os.Args[1]
content, err := FetchURL(url)
if err != nil {
fmt.Printf("Failed to fetch URL %s: %v\n", url, err)
os.Exit(1)
}
fmt.Printf("Content of %s:\n%s\n", url, content)
}
Step 4: Run the Program
Run the program using the go run
command.
go run main.go http://example.com
This will fetch and print the content of the specified URL.
Last modified: 19 December 2024