urlStr := "https://user:[email protected]/path" u, err := url.Parse(urlStr) if err != nil { log.Fatal(err) } fmt.Println(u.User.Username()) // "user" password, _ := u.User.Password() fmt.Println(password) // "pass"
u := &url.URL{ Scheme: "https", Host: "www.example.com", Path: "/path", User: url.UserPassword("user", "pass"), } fmt.Println(u.String()) // "https://user:[email protected]/path"In this example, we create a URL struct and set the User field to a UserPassword value containing the username and password. We then format the URL as a string and print it. Overall, these examples demonstrate how to use the URL User field to work with user information in URLs. The package library is net.url.