028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

GO的URL合法性检查

> 原文连接:[https://www.zhoubotong.site/post/67.html](https://www.zhoubotong.site/post/67.html) Go 标准库的net/url包提供的两个函可以直接检查URL合法性,不需要手动去正则匹配校验。 下面可以直接使用ParseRequestURI()函数解析URL,当然这个只会验证url格式,至于域名是否存在或注册,不会检查的,举个例子:
package main

import (
    "fmt"
    "net/url"
)

func main() {
    url, err := url.ParseRequestURI("https://www.zhoubotong.site") // 注意这里必须带有http/https协议,
    //否则会被认定非合法url,但是使用//www.zhoubotong.sit,被返回空,所以error哪里会被绕过,该示例代码不够严谨
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(url.Hostname())
} 

文章标题:GO的URL合法性检查
URL分享:http://whjierui.cn/article/dsoicjc.html

其他资讯