Golang url编码和字符转码
类似php中的urlencode 和htmlspecialchars:
package main import ( "fmt" "html" "net/url" "testing" ) func Test_Escape(t *testing.T) { //url编码 str := "中文-_." unstr := "%2f" fmt.Printf("url.QueryEscape:%s", url.QueryEscape(str)) fmt.Println() s, _ := url.QueryUnescape(unstr) fmt.Printf("url.QueryUnescape:%s", s) fmt.Println() //字符转码 hstr := "<" hunstr := "<" fmt.Printf("html.EscapeString:%s", html.EscapeString(hstr)) fmt.Println() fmt.Printf("html.UnescapeString:%s", html.UnescapeString(hunstr)) fmt.Println() }
you are the best!