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直接转

Go基础编程实践(二)—— 类型转换的更多相关文章

  1. Go基础编程实践(十)—— 数据库

    从数据库中读取数据 在http://sqlitebrowser.org/下载sqlite3可视化工具,在本main.go同目录下创建personal.db数据库,创建表如下: package main ...

  2. Go基础编程实践(九)—— 网络编程

    下载网页 package main import ( "io/ioutil" "net/http" "fmt" ) func main() ...

  3. Go基础编程实践(八)—— 系统编程

    捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...

  4. Go基础编程实践(七)—— 并发

    同时运行多个函数 观察常规代码和并发代码的输出顺序. // 常规代码,顺序执行,依次输出 package main import ( "fmt" "time" ...

  5. Go基础编程实践(六)—— 文件

    检查文件是否存在 在此程序同目录下创建log.txt文件,以检测. package main import ( "os" "fmt" ) func main() ...

  6. Go基础编程实践(五)—— 错误和日志

    自定义错误类型 Go中可以使用errors.New()创建错误信息,也可以通过创建自定义错误类型来满足需求.error是一个接口类型,所有实现该接口的类型都可以当作一个错误类型. // error类型 ...

  7. Go基础编程实践(四)—— 数组和map

    数组去重 package main import "fmt" func main(){ intSlice := []int{1,5,5,5,5,7,8,6,6, 6} fmt.Pr ...

  8. Go基础编程实践(三)—— 日期和时间

    日期和时间 package main import ( "fmt" "time" ) func main() { // 获取当前时间 current := ti ...

  9. Go基础编程实践(一)—— 操作字符串

    修剪空格 strings包中的TrimSpace函数用于去掉字符串首尾的空格. package main import ( "fmt" "strings" ) ...

随机推荐

  1. 如何优化SpringBoot的项目的启动速度

    日常开发SpringBoot项目启动类都用@SpringBootApplication,实际上它是下面三个注解的组合: @EnableAutoConfiguration: enable Spring ...

  2. ACM数据结构-树状数组

    模板: int n; int tree[LEN]; int lowbit(int x){ return x&-x; } void update(int i,int d){//index,del ...

  3. 新手AS常见问题集锦

    开发环境 以前开发android的时候可以使用eclipse,虽然现在也能使用eclipse,但是google已经不再支持使用eclipse开发android了.因为google有了自己的IDE--- ...

  4. 【CF1042F】Leaf Sets

    [CF1042F]Leaf Sets 题面 洛谷 题解 对于一个根节点\(x\),考虑其子树内的所有\(lca\)为它的叶子节点到它的距离\(d_1<d2<...<d_m\). 那么 ...

  5. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  6. SIT系统整合测试

    System Integrate Test的缩写,即系统整合测试      系统整合测试就是评估产品在其规格范围内的环境下工作,能否完成产品设计规格所需要的功能及与周边设备.应用软件的兼容性.大致可以 ...

  7. IO多路复用之Reactor

    参考文档: http://blog.csdn.net/u013074465/article/details/46276967 https://www.cnblogs.com/ivaneye/p/573 ...

  8. springboot(1)@SpringBootApplication

    首先来看下Spring Boot项目中的运行类,基本上每个项目都会有该启动类: @SpringBootApplication public class Application { public sta ...

  9. Spring Cloud Hystrix基本原理

    本篇学习Spring Cloud家族中的重要成员:Hystrix.分布式系统中一个服务可能依赖着很多其他服务,在高并发的场景下,如何保证依赖的某些服务如果出了问题不会导致主服务宕机这个问题就会变得异常 ...

  10. Research Guide: Pruning Techniques for Neural Networks

    Research Guide: Pruning Techniques for Neural Networks 2019-11-15 20:16:54 Original: https://heartbe ...