doushan2224 2018-06-27 18:41
浏览 75
已采纳

URL验证似乎已损坏

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?

  • 写回答

2条回答 默认 最新

  • duande1146 2018-06-27 18:49
    关注

    You have confused domains with URLs, the domain is only a part of the URL.

    Valid domain examples are: www.google.com, localhost and a.b.c.a.google.com.

    In order for a URL to be valid the scheme/protocol part (normally https://) must be there, see syntax at Wikipedia for an easy explanation.

    http://aaa is a valid URL by the same rules as http://localhost is valid

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题