In HTML, good study in Chinese can be expressed as good study
In the project, it is necessary to connect to the SMS alarm, and the data return required to be utf8
Later, I continued to communicate and found out that what I wanted was html-utf8;
No suitable golang toolkit was found, the main packages involved in language transcoding are
Mahonia, supports data conversion in various formats, gbk, utf8, gb2312
net/html supports web page transcoding, and the change is to convert the <,>,&,', in the web page.
Based on online experience, there are three versions of conversions:
Javascript
function ConvUtf8(obj) {returnobj.replace(/[^/u0000-/u00FF]/g,function($0) {returnescape($0).replace(/(%u)(/w{4})/gi, &#x$2;) });}JAVA
public static String UTF8_html_conv(String str){StringBuffer stbPreemptionArg = new StringBuffer();for(int i = 0;i<str.length();i++){if (str.codePointAt(i) > 255){stbPreemptionArg.append(&#x+Integer.toString(str.charAt(i), 16)+;);}else{stbPreemptionArg.append(str.charAt(i));}}return stbPreemptionArg.toString();}Golang
func CovertToHtml(src string) string{ rs := []rune(src) htmlUtf8 := for _, r := range rs { rint := int(r) if rint < 128 { htmlUtf8 += string(r) } else { //utf8 = //u+strconv.FormatInt(int64(rint), 16) htmlUtf8 += &#x+strconv.FormatInt(int64(rint), 16) + ; } }return htmlUtf8} SummarizeThe above is the method of converting Chinese to utf-8 in html introduced by the editor. I hope it will be helpful to everyone!