github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zstring/url.go (about)

     1  package zstring
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  )
     7  
     8  // UrlEncode url encode string, is + not %20
     9  func UrlEncode(str string) string {
    10  	return url.QueryEscape(str)
    11  }
    12  
    13  // UrlDecode url decode string
    14  func UrlDecode(str string) (string, error) {
    15  	return url.QueryUnescape(str)
    16  }
    17  
    18  // UrlRawEncode URL-encode according to RFC 3986.
    19  func UrlRawEncode(str string) string {
    20  	return strings.Replace(url.QueryEscape(str), "+", "%20", -1)
    21  }
    22  
    23  // UrlRawDecode Decode URL-encoded strings.
    24  func UrlRawDecode(str string) (string, error) {
    25  	return url.QueryUnescape(strings.Replace(str, "%20", "+", -1))
    26  }