import ( "net/http" ) func main() { req, err := http.NewRequest("GET", "http://example.com", nil) if err != nil { // handle error } req.SetBasicAuth("user123", "password456") // send HTTP request }
import ( "bytes" "net/http" ) func main() { body := bytes.NewBufferString("data to send") req, err := http.NewRequest("POST", "http://example.com", body) if err != nil { // handle error } req.SetBasicAuth("user123", "password456") // send HTTP request with body data }In both examples, the SetBasicAuth method is called on the request object before it is sent. The first parameter is the username, and the second parameter is the password. This method ensures that the HTTP request is authorized with the specified credentials. The SetBasicAuth method is part of the net/http package in Go Standard Library.