【go笔记】标准库-strconv
前言
标准库strconv提供了字符串类型与其他常用数据类型之间的转换。
strconv.FormatX()用于X类型转字符串,如strconv.FormatFloat()用于浮点型转字符串。strconv.ParseX()用于字符串转X类型,如strconv.ParseFloat()用于字符串转浮点型。- 对于整型,常用
strconv.Itoa()整型转字符串和strconv.Atoi()字符串转整型,当然也可以用FormatInt()和ParseInt()
函数原型
// 整型转字符串
func Itoa(i int) string
func FormatInt(i int64, base int) string
// 字符串转整型
func Atoi(s string) (int, error)
func ParseInt(s string, base int, bitSize int) (i int64, err error)
// 浮点型转字符串
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
// 字符串转浮点型
func ParseFloat(s string, bitSize int) (float64, error)
// 布尔型转字符串
func FormatBool(b bool) string
// 字符串转布尔型
func ParseBool(str string) (bool, error)
// 复数转字符串
func FormatComplex(c complex128, fmt byte, prec, bitSize int) string
// 字符串转复数
func ParseComplex(s string, bitSize int) (complex128, error)
具体函数说明请点击文末“参考资料”的官方文档链接。
示例代码
package main
import (
"fmt"
"strconv"
)
func main() {
// 整型转字符串
var a int = 123
// 输出:123, 123, string
fmt.Printf("%d, %v, %T \n", a, strconv.Itoa(a), strconv.Itoa(a))
// 浮点型转字符串
var b float64 = 3.141592653589793
// 输出:3.141593, 3.14159265, string
fmt.Printf("%f, %v, %T \n", b, strconv.FormatFloat(b, 'f', 8, 64), strconv.FormatFloat(b,'f', 8, 64))
// 字符串转整型
var c string = "56789"
if c2,err := strconv.Atoi(c); err == nil {
// 输出:string, 56789, int
fmt.Printf("%T, %v, %T \n",c, c2, c2)
}
// 字符串转浮点型
var d string = "123.456789"
if d2, err := strconv.ParseFloat(d, 64); err == nil {
// 输出:string, 123.456789, float64
fmt.Printf("%T, %v, %T \n", d, d2, d2)
}
}
参考资料
【go笔记】标准库-strconv的更多相关文章
- go学习笔记-标准库
标准库 名称 摘要 archive tar tar包实现了tar格式压缩文件的存取. zip zip包提供了zip档案文件的读写服务. bufio bufio 包实现了带缓存的I/O操作. built ...
- C++ primer笔记 -标准库类型
最重要的两个标准库类型:string和vector string 类型的输入操作符: 1.读取并忽略开头所有的空白符 2.读取字符直至再次遇到空白字符,读取终止 string对象的基本操作: stri ...
- Go笔记-标准库的介绍
[unsafe]包含了一些打破Go语言“类型安全”的命令,一般的程序中不会被使用,可用在C/C++程序的调用中 [syscall]底层的外部包,提供了操作系统底层调用的基本接口 [os/exec]提供 ...
- Go语言系列之标准库strconv
Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换. strconv包 strconv包实现了基本数据类型与其字符串表示的转换,主要有以下常用函数: Atoi().Itia().pa ...
- golang中的标准库strconv
strconv 包 strconv包实现了基本数据类型与其字符串表示的转换,主要有以下常用函数: Atoi().Itia().parse系列.format系列.append系列. string与int ...
- C++ Primer 笔记——标准库类型string
1.如果使用等号初始化一个变量,实际上执行的是拷贝初始化,编译器吧等号右侧的初始值拷贝到新创建的对象中去:如果不使用等号则执行的是直接初始化. std::string str = "Test ...
- python笔记-标准库unittest
unittest核心工作原理 unittest中最核心的四个概念是:test case, test suite, test runner, test fixture. 一个TestCase的实例就是一 ...
- PHP标准库 (SPL) 笔记
简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...
- STL笔记(6)标准库:标准库中的排序算法
STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...
- C++标准库第二版笔记 3 和异常的理解 1
C++标准库第二版笔记 3 和异常的理解 1 差错和异常(error and exception)的处理 标准异常类(exception class) 定义于 分为: 1.语言本身支持的异常 2.标准 ...
随机推荐
- 2020-11-25:go中,map的底层数据结构是什么?
福哥答案2020-11-25: 简单回答:hmap映射头.bmap桶.mapextra溢出额外信息 中级回答: // 映射头 type hmap struct { // Note: the forma ...
- uni-app 创建项目及目录结构
文件-新建-1.项目 ┌─uniCloud 云空间目录,阿里云为uniCloud-aliyun,腾讯云为uniCloud-tcb(详见uniCloud) │─components 符合vue组件规范的 ...
- ICML 2017-Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
Key Gradient Descent+TRPO+policy Gradient 训练模型的初始参数,模型在新任务上只需参数通过一个或多个用新任务的少量数据计算的梯度步骤更新后,就可以最大的性能.而 ...
- Java 网络编程 —— 实现非阻塞式的服务器
创建阻塞的服务器 当 ServerSocketChannel 与 SockelChannel 采用默认的阻塞模式时,为了同时处理多个客户的连接,必须使用多线程 public class EchoSer ...
- Prompt Engineering优化原则 - 以Webshell代码解释为例
一.LLM prompt优化原则 本文围绕"PHP代码解释"这一任务,讨论LLM prompt优化原则. 代码样例如下: <?php echo "a5a5aa555 ...
- 2023-05-25:给定一个正整数 x,我们将会写出一个形如 x (op1) x (op2) x (op3) x ... 的表达式 其中每个运算符 op1,op2,… 可以是加、减、乘、除之一 例如
2023-05-25:给定一个正整数 x,我们将会写出一个形如 x (op1) x (op2) x (op3) x ... 的表达式 其中每个运算符 op1,op2,- 可以是加.减.乘.除之一 例如 ...
- hexrays sdk study
There are 20 examples in /ida_path/plugins/hexrays_sdk/plugins, you can learn from that, you can als ...
- THM红队基础
Red Team Fundamentals Learn the core components of a red team engagement, from threat intelligence t ...
- unity添加Mysql的dll以及发布的问题
最近在做一个unity项目中,要读取数据库,还是MySql的数据库.遇到了很多问题,写出来供大家参考一下. 关于unity引用第三方的Mysql.data.dll的问题: 这个地方有一个难点,正常的C ...
- ChatGPT之问艺道:如何借助神级算法Prompt,让你轻松get到更高质量答案?
摘要:本文由葡萄城技术团队编写,文章的内容借鉴于Ibrahim John的<The Art of Asking ChatGPT>(向ChatGPT提问的艺术). 前言 当今,ChatGPT ...