url_test.go 680 Bytes
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package test

import (
	"fmt"
	"net/url"
	"path"
	"strings"
	"testing"
)

func TestUrlParse(t *testing.T) {
	//rawUrl := "https://pkg.go.dev/net/url?a=b&c=d#example-ParseQuery"
	rawUrl := "https://pkg.go.dev/net/url"
	uri, err := url.Parse(rawUrl)
	if err != nil {
		return
	}
	query := uri.RawQuery
	queryArr := strings.Split(query, "&")
	v := url.Values{}
	if len(queryArr) > 0 {
		for _, q := range queryArr {
			if arr := strings.Split(q, "="); len(arr) == 2 {
				v.Add(arr[0], arr[1])
			}
		}
	}
	v.Add("v1", "v1")
	v.Add("v2", "v2")
	uri.RawQuery = v.Encode()
	uri.Path = path.Join(uri.Path, "/ccc/ddd")
	fmt.Println(fmt.Sprintf("修改后的URL是:%s", uri.String()))

}