GO 笔记

string 转 map

string.(map[string]interface{})

0730

strconv 参考:Package strconv

包strconv实现了对基本数据类型的字符串表示的转换

  • 数值转换
    Atoi(string to int ) Itoa(int to String)
i, err := strconv.Atoi("-42")
s := strconv.Itoa(-42)

ParseBool, ParseFloat, ParseInt, and ParseUint convert strings to values:

b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)

FormatBool, FormatFloat, FormatInt, and FormatUint convert values to strings

s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

string Conversions

Quote and QuoteToASCII convert strings to quoted Go string literals. The latter guarantees that the result is an ASCII string, by escaping any non-ASCII Unicode with \u:
Quote 和 QuoteToASCII 将字符串转换为带引号的字符串文字。后者保证结果是一个 ASCII 字符串,用 \ u 转义任何非 ASCII 的 Unicode

q := strconv.Quote("Hello, 世界")
q := strconv.QuoteToASCII("Hello, 世界")
  • 字符串转换

rune

官方定义

等同于int32,主要来区分字符串值和整数值

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32

demo


func main()  {
   var str="hello 世界"

   //得到字符串的字节长度 (12)
   //golang 中string 底层是通过bytes 数组实现的,中文字符在unicode下占2个字节,在utf-8编码下占3个字节,而golang默认编码正好是utf-8
   fmt.Println(len(str))

   //得到字符串的长度 (8)
   l:=len([]rune(str))
   fmt.Println(l)

   //go中的unicode/utf8包提供了用utf-8获取长度的方法 (8)
   l2:=utf8.RuneCountInString(str)
   fmt.Println(l2)

}

defer

##demo

package main

import "fmt"

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值