urlKit

package
v3.0.913 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DecodeURIComponent func(s string) (string, error) = url.QueryUnescape

DecodeURIComponent 解码.

e.g. ("") => "", nil

View Source
var EncodeURIComponent func(s string) string = url.QueryEscape

EncodeURIComponent 编码.

!!!: 事实上,"不完全"等同于js的 encodeURI || encodeURIComponent.

e.g. golang ("") => "" ("test 测试") => "test+%E6%B5%8B%E8%AF%95"

e.g.1 js encodeURIComponent("test 测试") => "test%20%E6%B5%8B%E8%AF%95"

View Source
var Parse func(rawURL string) (*url.URL, error) = url.Parse

Parse 解析url(http、https、rtsp、rtmp等协议)

PS: (1) URL.EscapedPath(): 处理过的 path (2) URL.EscapedFragment(): 处理过的 fragment(# 后面的玩意) (3) Values.Encode(): 处理过的 query string (4) url.Parse vs url.ParseRequestURI: 当要解析的url字符串中包含有字符“#”时,使用url.Parse解析,会导致#后面的参数解析不出来。而使用ParseRequestURI就能解析到.

使用场景:
	(a) rawURL中必定不存在'#',使用:	url.Verify
	(b) rawURL中可能存在'#',使用:		url.ParseRequestURI

GoLand教程-Go URL解析

https://mp.weixin.qq.com/s/i6uEUzvu5BPna5QtSYDSMQ

golang url.Parse 解析

https://blog.csdn.net/zhuyuqiang1238/article/details/121807708

@param rawURL !!!不能是如下格式: "localhost:8080"

e.g.

u, err := urlKit.Verify("http://localhost:8080/go?a=123&b=456")
if err != nil {
	panic(err)
}
fmt.Println(u.Scheme)     // http
fmt.Println(u.Host)       // localhost:8080
fmt.Println(u.Hostname()) // localhost
fmt.Println(u.Port())     // 8080
fmt.Println(u.Path)       // /go
fmt.Println(u.RawQuery)   // a=123&b=456
fmt.Println(u.Query())    // map[a:[123] b:[456]]
View Source
var ParseQuery func(query string) (url.Values, error) = url.ParseQuery

ParseQuery

e.g.

u, err := urlKit.Verify("http://localhost:8080/go?a=123&b=456")
if err != nil {
	panic(err)
}
fmt.Println(u.RawQuery) // a=123&b=456

m, err := urlKit.ParseQuery(u.RawQuery)
if err != nil {
	panic(err)
}
fmt.Println(m) 			// map[a:[123] b:[456]]
View Source
var ParseRequestURI func(rawURL string) (*url.URL, error) = url.ParseRequestURI
View Source
var PolyfillUrl func(reqUrl string, queryParams map[string][]string) (string, error) = AddQueryParamsToUrl

PolyfillUrl 优化url(类似Chrome浏览器,地址栏中的url有问题,但复制出来的是好的).

@param queryParams 额外的query参数, (1) 没有可传nil

(2) 值中切片中的字符串应当是未处理(编码)过的

Functions

func AddQueryParamsToUrl

func AddQueryParamsToUrl(rawURL string, queryParams map[string][]string) (string, error)

func AddQueryParamsToValues

func AddQueryParamsToValues(values url.Values, queryParams map[string][]string) url.Values

AddQueryParamsToValues

@param queryParams 切片中的字符串应当是未处理(编码)过的 @return 必定不为nil

func AddToRawQuery added in v3.0.33

func AddToRawQuery(u *url.URL, queryParams map[string][]string)

AddToRawQuery 增添请求的query.

PS: 可能会修改 req.URL.RawQuery.

@param queryParams 切片中的字符串应当是未处理(编码)过的

func OverrideRawQuery added in v3.0.33

func OverrideRawQuery(u *url.URL, queryParams map[string][]string)

OverrideRawQuery 覆盖请求的query.

PS: 可能会修改 req.URL.RawQuery.

func ToEscapedQueryString

func ToEscapedQueryString[T ~map[string][]string](m T) string

ToEscapedQueryString

@return 处理过的query string(可直接放到url中)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL