bool to string

strconv包的FormatBool函数用于将bool转为string

package main

import (
"fmt"
"strconv"
) func main() {
isNew := true
isNewStr := strconv.FormatBool(isNew)
// message := "Purchased item is " + isNew 会报错,类型不匹配
message := "Purchased item is " + isNewStr fmt.Println(message)
}

int/float to string

strconv包的FormatIntFormatFloat函数用于将int、float转为string

package main

import (
"fmt"
"strconv"
) func main() {
// Int to String
numberInt := int64(20)
numberItoS := strconv.FormatInt(numberInt, 8)
fmt.Println(numberItoS) // Float to String
numberFloat := 177.12211
// FormatFloat函数第二个参数表示格式,例如`e`表示指数格式;
// 第三个参数表示精度,当你想要显示所有内容但又不知道确切位数可以设为-1。
numberFtoS := strconv.FormatFloat(numberFloat, 'f', 3, 64)
fmt.Println(numberFtoS)
}

string to bool

strconv包的ParseBool函数用于将string转为bool

package main

import (
"fmt"
"strconv"
) func main() {
isNew := "true"
isNewBool, err := strconv.ParseBool(isNew)
if err != nil {
fmt.Println("failed")
} else {
if isNewBool {
fmt.Println("IsNew")
} else {
fmt.Println("Not New")
}
}
}
// ParseBool函数只接受1、0、t、f、T、F、true、false、True、False、TRUE、FALSE,其他值均返回error

string to int/float

strconv包的ParseIntParseFloat函数用于将string转为int、float

package main

import (
"fmt"
"strconv"
) func main() {
// string to int
numberI := "2"
numberInt, err := strconv.ParseInt(numberI, 10, 32)
if err != nil {
fmt.Println("Error happened")
} else {
if numberInt == 2 {
fmt.Println("Success")
}
} // string to float
numberF := "2.2"
numberFloat, err := strconv.ParseFloat(numberF, 64)
if err != nil {
fmt.Println("Error happened")
} else {
if numberFloat == 2.2 {
fmt.Println("Success")
}
}
}

[]byte to string

在Go中,string的底层就是[]byte,所以之间的转换很简。

package main

import "fmt"

func main() {
helloWorld := "Hello, World"
helloWorldByte := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100}
fmt.Println(string(helloWorldByte), []byte(helloWorld))
// fmt.Printf("%q", string(helloWorldByte))
}

总结

  • 转成stringFormat
  • string转其它用Parse
  • string[]byte直接转

最新文章

  1. sql 截取日期
  2. Android开发自学笔记(Android Studio)—4.1布局组件
  3. asp.net form身份认证不定时认证失败的问题 排查
  4. 2015最新德淘W家(Windeln.de)新人优惠码wcode0520,赠1000积分,可抵10欧元
  5. 使用Immutable优化复制注意事项
  6. DataGrid实现逻辑分页
  7. Oracle EBS-SQL (SYS-19):sys-用户登陆纪录查询.sql
  8. java.lang.NoSuchMethodError:android.content.Context.getDrawable
  9. John Deere Service Advisor with Nexiq clone 90% Worked
  10. ProtocolBuffer for Objective-C Mac运行环境配置
  11. 实现一个简易版RPC
  12. 使用pycharm调试django项目
  13. IHttpHandler IHttpModule
  14. Verdi文档路径
  15. linux -- ubuntu 脚本开机自启动
  16. while(scanf("%d",&n)!=EOF)与while(cin>>n)
  17. 2017ACM暑期多校联合训练 - Team 6 1002 HDU 6097 Mindis (数学)
  18. (转)spring IOC、DI理解
  19. javascript进阶修炼之一——javascript必备操做
  20. PL/SQL: numeric or value error: character to number conversion error

热门文章

  1. ES6 String和Number扩展
  2. js时间练习
  3. GET /static/css/bootstrap.min.css.map HTTP/1.1" 404
  4. [CMS] UsualToolCMS-8.0 sql注入漏洞【转载】
  5. Linux搭建简单的http文件服务器111
  6. 简单find命令的实现
  7. 【Beta】Phylab 发布说明
  8. 南开大学2020年数学分析高等代数考研试题回忆版TeX排版
  9. mac 下面用dd 制作u盘启动
  10. hive删除空分区