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. 【ARC098F】Donation

    [ARC098F]Donation 题面 atcoder 题意: 给定一张\(n\)个点,\(m\)条边的无向图.这张图的每个点有两个权值 \(a_i,b_i\). 你将会从这张图中选出一个点作为起点 ...

  2. CGLIB和Java动态代理的区别(笔记)

    java常用知识点: 1.Java动态代理只能够对接口进行代理,不能对普通的类进行代理(因为所有生成的代理类的父类为Proxy,Java类继承机制不允许多重继承):CGLIB能够代理普通类:2.Jav ...

  3. webpack4.0构建项目流程

    webpack4.0构建项目流程,具体的就不一一唠叨了,这里给出构建流程步骤: 流程大图: 下载高清大图

  4. test软件工程第三次作业

    零.前言 本次作业要求个人编写程序,截止日期2019年9月25日23:00. 请先阅读<构建之法>第一章至第三章的内容,并在下方作业里体现出阅读后的成果.特别是第2章中的效能分析及个人软件 ...

  5. MIME类型和Java类型

    MIME类型和Java类型 类型转换Spring Cloud Stream提供的开箱即用如下表所示:“源有效载荷”是指转换前的有效载荷,“目标有效载荷”是指转换后的“有效载荷”.类型转换可以在“生产者 ...

  6. 我的node-webkit笔记

    话不多说,直接上码: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  7. HSBImageView--android--可以设置HSB值的imageview

    package guide.yunji.com.guide.view; import android.content.Context; import android.content.res.Typed ...

  8. Android之WebRTC介绍

    参考自:Introduction to WebRTC on AndroidAndroid之WebRTC介绍 WebRTC被誉为是web长期开源开发的一个新启元,是近年来web开发的最重要创新.WebR ...

  9. Better ultra_simple for Slamtec RPLIDAR on Linux

    Improved the ultra_simple program to visualize the samples with GLUT on Linux, tested with Slamtec R ...

  10. LeetCode 101. Symmetric Tree(镜像树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...