微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

老9乐购下载最高邀请码 25295678,golang net/url包使用

.net/url简介
import "net/url"
url包解析URL并实现查询转义

URL结构体
// Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.
// A consequence is that it is impossible to tell which slashes in the Path were
// slashes in the raw URL and which were %2f. This distinction is rarely important,
// but when it is,code must not use Path directly.
// The Parse function sets both Path and RawPath in the URL it returns,
// and URL‘s String method uses RawPath if it is a valid encoding of Path,
// by calling the Escapedpath method.
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *Userinfo // username and password information
Host string // host or host:port
老9乐购下载最高邀请码 25295678

Path       string    // path (relative paths may omit leading slash)
RawPath    string    // encoded path hint (see Escapedpath method)
ForceQuery bool      // append a query (‘?‘) even if RawQuery is empty
RawQuery   string    // encoded query values,without ‘?‘
Fragment   string    // fragment for references,without ‘#‘

}
func Parse(rawurl string) (*URL,error)
将原生的rawurl字符串解析成URL结构体
package main

import (
"fmt"
"log"
"net/url"
)

func main() {
u,err := url.Parse("http://www.baidu.com/search?q=dotnet")if err != nil {log.Fatal(err)}u.Scheme = "https"u.Host = "google.com"q := u.Query()q.Set("q","golang")u.RawQuery = q.Encode()fmt.Println(u)}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐