当前位置:网站首页>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)
}
边栏推荐
- eBPF Cilium实战(1) - 基于团队的网络隔离
- Opencv learning notes 1 -- several methods of reading images
- GFS分布式文件系统
- Le système mes est un choix nécessaire pour la production de l'entreprise
- 探索STEAM艺术设计中的创造力
- 解读创客思维与数学课程的实际运用
- 数据中台落地实施之法
- Easy to understand SSO
- A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
- A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
猜你喜欢
Tuowei information uses the cloud native landing practice of rainbow
在 Rainbond 中一键安装高可用 Nacos 集群
2-3查找樹
使用SwinUnet训练自己的数据集
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
The field value in Splunk subquery fuzzy matching CSV is*
Wang Zijian: is the NFT of Tencent magic core worth buying?
Bisenet features
The single value view in Splunk uses to replace numeric values with text
Golang compilation constraint / conditional compilation (/ / +build < tags>)
随机推荐
MES系統,是企業生產的必要選擇
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Iptables' state module (FTP service exercise)
Opencv learning notes 1 -- several methods of reading images
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Opencv learning note 4 - expansion / corrosion / open operation / close operation
Input and output of floating point data (C language)
饥荒云服管理脚本
在Rainbond中一键部署高可用 EMQX 集群
在Rainbond中实现数据库结构自动化升级
Analysis of maker education in innovative education system
In go language, function is a type
【雅思口语】安娜口语学习记录 Part2
如何理解分布式架构和微服务架构呢
解析创新教育体系中的创客教育
Merge sort and non comparison sort
2-3 lookup tree
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
BiSeNet的特点
[kuangbin]专题十五 数位DP