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)


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

查看所有标签

猜你喜欢:

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

Learning Web App Development

Learning Web App Development

Semmy Purewal / O'Reilly Media / 2014-3-3 / USD 29.99

Grasp the fundamentals of web application development by building a simple database-backed app from scratch, using HTML, JavaScript, and other open source tools. Through hands-on tutorials, this pract......一起来看看 《Learning Web App Development》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具