golang 数据类型之间的转换

栏目: Go · 发布时间: 6年前

内容简介:一、基本数据类型之间的转换1、string到intint,err:=strconv.Atoi(string)

一、基本数据类型之间的转换

1、string到int

int,err:=strconv.Atoi(string)

2、string到int64

int64, err := strconv.ParseInt(string, 10, 64)

3、int到string

string:=strconv.Itoa(int)

4、int64到string

string:=strconv.FormatInt(int64,10)

5、字符串到float32/float64

float32, err = ParseFloat(string, 32)

float64,err = ParseFloat(string,64)

6、int64转int

int:=int(int64)

7、int转int64

int64:=int64(int)

二、interface{}与其他类型之间的转换

转换方式包括隐式转换与断言转换。

1、interface{}类型转换成具体类型:interfaceVar.(具体类型)

原理:断言实现。如:

断言成功返回true,失败返回false

value, ok := a.(string)
if !ok {
    fmt.Println("It's not ok for type string")
    return
}
fmt.Println("The value is ", value)

2、具体类型可以隐式转换成interface{}类型

3、string与[]byte之间的转换:

string到[]byte:字节数组=[]byte(字符串)

字节数组到string: 字符串=string([]byte)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithm Analysis in Java

Data Structures and Algorithm Analysis in Java

Mark A. Weiss / Pearson / 2011-11-18 / GBP 129.99

Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course wa......一起来看看 《Data Structures and Algorithm Analysis in Java》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具