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 := "&lt"
    fmt.Printf("html.EscapeString:%s", html.EscapeString(hstr))
    fmt.Println()
    fmt.Printf("html.UnescapeString:%s", html.UnescapeString(hunstr))
    fmt.Println()
}

 

posted @ 2019-01-30 11:55  林锅  阅读(7843)  评论(0编辑  收藏  举报