How to perform HTTP basic authentication in Go client requests?

In Go, you can perform HTTP basic authentication in client requests by setting the Authorization header with the encoded username and password. Here's an example:

package main import ( "fmt" "net/http" "encoding/base64" ) func main() { // Create a new HTTP client client := &http.Client{} // Create a new request req, err := http.NewRequest("GET", "https://api.example.com/endpoint", nil) if err != nil { fmt.Println("Error creating request:", err) return } // Set the Authorization header username := "your_username" password := "your_password" auth := username + ":" + password basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) req.Header.Set("Authorization", basicAuth) // Send the request resp, err := client.Do(req) if err != nil { fmt.Println("Error sending request:", err) return } defer resp.Body.Close() // Process the response fmt.Println("Response status:", resp.Status) // ... }

Replace "your_username" and "your_password" with your actual credentials.