commit
6cf4ec89f3
@ -0,0 +1,17 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: clone
|
||||
|
||||
steps:
|
||||
- name: Test
|
||||
image: golang:1.18
|
||||
commands:
|
||||
- go env -w GO111MODULE=on
|
||||
- go env -w GOPROXY=https://goproxy.cn,direct
|
||||
- go test -v ./...
|
||||
- name: Benchmark
|
||||
image: golang:1.18
|
||||
commands:
|
||||
- go env -w GO111MODULE=on
|
||||
- go env -w GOPROXY=https://goproxy.cn,direct
|
||||
- go test -bench=. -benchmem
|
@ -0,0 +1,7 @@
|
||||
.env
|
||||
.git
|
||||
.svn
|
||||
.idea
|
||||
.vscode
|
||||
.log
|
||||
gomod.sh
|
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 李光春
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,17 @@
|
||||
<h1>
|
||||
<a href="https://www.dtapp.net/">Golang Url</a>
|
||||
</h1>
|
||||
|
||||
📦 Golang Url
|
||||
|
||||
[comment]: <> (go)
|
||||
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gourl?status.svg)](https://pkg.go.dev/go.dtapp.net/gourl)
|
||||
[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gourl/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gourl)
|
||||
[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gourl)](https://goreportcard.com/report/go.dtapp.net/gourl)
|
||||
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgourl)
|
||||
|
||||
#### 安装
|
||||
|
||||
```go
|
||||
go get -v -u go.dtapp.net/gourl
|
||||
```
|
@ -0,0 +1,70 @@
|
||||
package gourl
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ResponseUrlParse 返回参数
|
||||
type ResponseUrlParse struct {
|
||||
Uri string `json:"uri"` // URI
|
||||
Urn string `json:"urn"` // URN
|
||||
Url string `json:"url"` // URL
|
||||
Scheme string `json:"scheme"` // 协议
|
||||
Host string `json:"host"` // 主机
|
||||
Hostname string `json:"hostname"` // 主机名
|
||||
Port string `json:"port"` // 端口
|
||||
Path string `json:"path"` // 路径
|
||||
RawQuery string `json:"raw_query"` // 参数 ?
|
||||
Fragment string `json:"fragment"` // 片段 #
|
||||
}
|
||||
|
||||
// UriParse 解析URl
|
||||
func UriParse(input string) (resp ResponseUrlParse) {
|
||||
parse, err := url.Parse(input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp.Uri = input
|
||||
resp.Urn = parse.Host + parse.Path
|
||||
resp.Url = parse.Scheme + "://" + parse.Host + parse.Path
|
||||
resp.Scheme = parse.Scheme
|
||||
resp.Host = parse.Host
|
||||
resp.Hostname = parse.Hostname()
|
||||
resp.Port = parse.Port()
|
||||
resp.Path = parse.Path
|
||||
resp.RawQuery = parse.RawQuery
|
||||
resp.Fragment = parse.Fragment
|
||||
return
|
||||
}
|
||||
|
||||
// UriFilterExcludeQueryString 过滤掉url中的参数
|
||||
func UriFilterExcludeQueryString(uri string) string {
|
||||
URL, _ := url.Parse(uri)
|
||||
clearUri := strings.ReplaceAll(uri, URL.RawQuery, "")
|
||||
clearUri = strings.TrimRight(clearUri, "?")
|
||||
return strings.TrimRight(clearUri, "/")
|
||||
}
|
||||
|
||||
// LenCode 编码
|
||||
func LenCode(s string) string {
|
||||
escape := url.QueryEscape(s)
|
||||
return escape
|
||||
}
|
||||
|
||||
// DeCode 解码
|
||||
func DeCode(s string) string {
|
||||
unescape, _ := url.QueryUnescape(s)
|
||||
return unescape
|
||||
}
|
||||
|
||||
// ParseQuery 获取URL参数 https://studygolang.com/articles/2876
|
||||
func ParseQuery(s string) map[string][]string {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
urlParam := u.RawQuery
|
||||
m, _ := url.ParseQuery(urlParam)
|
||||
return m
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package gourl
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUrlParse(t *testing.T) {
|
||||
t.Logf("%+v", UriParse("https://api.dtapp.net/ip?ip=127.0.0.1#history"))
|
||||
t.Logf("%+v", UriParse("https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance&compact=false#history"))
|
||||
t.Logf("%+v", UriFilterExcludeQueryString("/"))
|
||||
t.Logf("%+v", UriFilterExcludeQueryString("/favicon.ico"))
|
||||
t.Logf("%+v", UriFilterExcludeQueryString("/ip"))
|
||||
t.Logf("%+v", UriFilterExcludeQueryString("/ip?ip=127.0.0.1"))
|
||||
}
|
Loading…
Reference in new issue