I'm trying to validate a URL using Go's standard library. This is what my code currently looks like.
import (
"fmt"
"net/url"
)
func isValidURL(tocheck string) bool {
_, err := url.ParseRequestURI(tocheck)
return err == nil
}
func main() {
fmt.Println(isValidURL("google.com")) //returns false, expected true
fmt.Println(isValidURL("www.google.com")) //returns false, expected true
fmt.Println(isValidURL("google")) //returns false, expected false
fmt.Println(isValidURL("/google")) //returns true, expected false
}
All three examples print false, even though the first two should be true. I then tried appending https://
to the beginning of URLs that don't start with them, but then everything, like https://aaaa
is parsed as valid. What can I do to make sure it only returns true when the URL is actually valid?