golang 数据类型之间的转换

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

内容简介:一、基本数据类型之间的转换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)


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

查看所有标签

猜你喜欢:

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

HTML5

HTML5

Matthew David / Focal Press / 2010-07-29 / USD 39.95

Implement the powerful new multimedia and interactive capabilities offered by HTML5, including style control tools, illustration tools, video, audio, and rich media solutions. Understand how HTML5 is ......一起来看看 《HTML5》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具