主要内容
主要根据自己在开发过程中的使用经验,总结一下常用的基础包的使用方式;
可能会有疏漏和错误,主要是强迫自己来进行总结;
归纳列表
1.string & strconv
strconv – 数字转换
strconv包最常用的是整型,浮点型,布尔等类型和字符串的转换;
FormatBool -- "true"/"false"
FormatFloat
FormatInt
FormatUint
ParseBool
ParseFloat
ParseInt
ParseUint
Itoa -- 转字符串
Atoi -- 转int
## 其中FormatFloat的使用细节,可以简化大部分浮点数的格式化问题
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
f: 需要格式化的浮点数
fmt: 格式化表述,如下
prec: 精度(小数位数)
bitSize: 保存字节大小(64->32可能会进行四舍五入),特别的是,-1让函数自己判断需要使用的位数
fmt格式化:
'b' : -ddddp±ddd, 二进制指数
'e'/'E' : -d.dddde±dd/-d.ddddE±dd,十进制指数
'f' : -ddd.dddd,非指数
'g'/'G' : 对于大数相当于'e'/'E' ,否则和'f'一样
- strconv – 字符串转换*
其实strconv来做字符串转换的时间很少,主要包含quote,ascii相关转换;以及将数字转换和字符串转换相结合的AppendXXX函数;
func Quote(s string) string
func QuoteRune(r rune) string
func QuoteRuneToASCII(r rune) string
func QuoteRuneToGraphic(r rune) string
func QuoteToASCII(s string) string
func QuoteToGraphic(s string) string
func Unquote(s string) (string, error)
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
func AppendBool(dst []byte, b bool) []byte
func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
func AppendInt(dst []