当前位置:网站首页>Basic data types and string types are converted to each other
Basic data types and string types are converted to each other
2022-07-07 08:32:00 【Jimmy_ jimi】
Basic data type string
Method 1 :fmt.Sprintf format
import "fmt"
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
//int To string
str := fmt.Sprintf("%d", num1)
fmt.Printf("str type : %T , str=%q", str, str)
//float64 To string
str = fmt.Sprintf("%f", num2)
fmt.Printf("str type : %T , str=%q", str, str)
//bool To string
str = fmt.Sprintf("%t", b)
fmt.Printf("str type : %T , str=%q", str, str)
//byte To string
str = fmt.Sprintf("%c", myChar)
fmt.Printf("str type : %T , str=%q", str, str)
}
// str type : string , str="99"
// str type : string , str="123.120000"
// str type : string , str="true"
// str type : string , str="h"
Method 2 :strconv Function of the package
import (
"fmt"
"strconv"
)
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
// Mode one 、int To string
str := strconv.FormatInt(int64(num1), 10)
fmt.Printf("str type : %T , str=%q", str, str)
// Mode two 、int To string
str = strconv.Itoa(num1)
fmt.Printf("str type : %T , str=%q", str, str)
//float64 To string
str = strconv.FormatFloat(num2, 'f', 10, 64)
fmt.Printf("str type : %T , str=%q", str, str)
//bool To string
str = strconv.FormatBool(b)
fmt.Printf("str type : %T , str=%q", str, str)
//byte To string
//str = strconv.Itoa(int(myChar))
str = string(myChar)
fmt.Printf("str type : %T , str=%q", str, str)
}
// str type : string , str="99"
// str type : string , str="99"
// str type : string , str="123.1200000000"
// str type : string , str="true"
// str type : string , str="h"
string Type to basic data type
package main
import (
"fmt"
"strconv"
)
func main() {
var num1 string = "99"
var num2 string = "123.12"
var b string = "true"
// string To int64
//10 Represents the hexadecimal after output ,2-32 Base number
// here 64 Is the limit bit , Not the data type of the specified output , in fact strconv.ParseInt Only return int64 Type data ,bitesize The limit is the data size , If the transformed shaping data num1 exceed 127, that err It will show that the data is out of range ( Report errors (variable of type int64) as type int in argument to fbn Cause analysis ).
str, _ := strconv.ParseInt(num1, 10, 64)
fmt.Printf("str type: %T str=%d", str, str)
// string To float64
number, _ := strconv.ParseFloat(num2, 64)
fmt.Printf("str type: %T str=%f", number, number)
// string To bool
ret, _ := strconv.ParseBool(b)
fmt.Printf("str type: %T str=%t", ret, ret)
}
边栏推荐
- Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
- MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
- Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
- Standard function let and generic extension function in kotlin
- Kotlin combines flatmap for filtering and zip merge operators
- Snyk dependency security vulnerability scanning tool
- 利用 Helm 在各类 Kubernetes 中安装 Rainbond
- 如何理解分布式架构和微服务架构呢
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- 解析创新教育体系中的创客教育
猜你喜欢
Tuowei information uses the cloud native landing practice of rainbow
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
Application of slip ring of shipborne radar antenna
Implementation of navigation bar at the bottom of applet
Implement your own dataset using bisenet
[IELTS speaking] Anna's oral learning records part2
单元测试报告成功率低
iptables 之 state模块(ftp服务练习)
一文了解如何源码编译Rainbond基础组件
GFS分布式文件系统
随机推荐
2-3 lookup tree
Interpreting the practical application of maker thinking and mathematics curriculum
BiSeNet的特點
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
SSM integration
Xcit learning notes
使用SwinUnet训练自己的数据集
PLSQL的安装和配置
Le système mes est un choix nécessaire pour la production de l'entreprise
Go语言中,函数是一种类型
解析机器人科技发展观对社会研究论
Opencv learning notes II - basic image operations
POJ - 3784 Running Median(对顶堆)
解析创新教育体系中的创客教育
【无标题】
Explore creativity in steam art design
In go language, function is a type
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
Low success rate of unit test report
Deit learning notes