golang 有很多需要将中文转成utf8的

网上搜到一个直接转的,记录下,备用

package main

import "golang.org/x/text/encoding/simplifiedchinese"

type Charset string

const (
   UTF8    = Charset("UTF-8")
   GB18030 = Charset("GB18030")
)

func ConvertByte2String(byte []byte, charset Charset) string {

   var str string
   switch charset {
   case GB18030:
      var decodeBytes,_=simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
      str= string(decodeBytes)
   case UTF8:
      fallthrough
   default:
      str = string(byte)
   }

   return str
}

 

相关文章:

  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2021-06-16
  • 2021-08-03
  • 2021-08-29
  • 2021-11-27
  • 2022-12-23
相关资源
相似解决方案