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. Redis 迁移 DB; move key db

    redis 移动 DB MOVE key db将当前数据库的 key 移动到给定的数据库 db 当中.如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定 key ,或者 key 不存 ...

  2. .net core 从 ActionFilterAttribute 获取Request.Body 的正确方式

    由于 ModelBinding在动作过滤器之前运行,直接使用 context.ActionArguments["parameter"]  获取模型对象 This article s ...

  3. 常用方法 DataTable转换为Entitys

    备注:摘自网上 有附地址 public static List<T> DataTableToEntities<T>(this DataTable dt) where T : c ...

  4. Spring中静态方法中使用@Resource注解的变量

    开发中,有些时候可能会工具类的静态方法,而这个静态方法中又使用到了@Resource注解后的变量.如果要直接使用 Utils.staticMethod(),项目会报异常:如果不直接使用,还要先 new ...

  5. 修改git 的远程URL

    git remote set-url origin ssh://git@gitlab.tian-wang.com:8022/test/api-automation.git

  6. vs2017+qt5.x编译32位应用<转>

    原文地址:https://www.cnblogs.com/woniu201/p/10862170.html 概述 最近有同学私信我,问如何使用vs2017+qt5.10编译出32位的应用,需要使用ms ...

  7. OpenGL的核心模式与立即渲染模式

    早期的OpenGL使用立即渲染模式(Immediate mode,也就是固定渲染管线),这个模式下绘制图形很方便.OpenGL的大多数功能都被库隐藏起来,开发者很少能控制OpenGL如何进行计算的自由 ...

  8. yapi内网部署 centos

    1.部署方案 官方说明: https://hellosean1025.github.io/yapi/devops/index.html 2.需要注意的点 (1)在centos等服务启上最好使用“命令行 ...

  9. Spark动态资源分配-Dynamic Resource Allocation

    微信搜索lxw1234bigdata | 邀请体验:数阅–数据管理.OLAP分析与可视化平台 | 赞助作者:赞助作者 Spark动态资源分配-Dynamic Resource Allocation S ...

  10. SNF快速开发平台2019-用户安全控制-权限管理模型实践-权限都在这里

    1.1    是否保存密码 勾选记住密码后,再次开启程序用户密码不需要再次输入,直接显示在密码输入框内,方便快捷. 图 4.1‑1 记住密码的登录页面框 1.2    是否自动登录 勾选自动登录后,再 ...