4

go语言int类型转化成string类型的方式

 2 years ago
source link: https://studygolang.com/articles/3507?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

go语言int类型转化成string类型的方式

love_se · 2015-06-17 20:06:14 · 9852 次点击 · 预计阅读时间不到 1 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2015-06-17 20:06:14 的文章,其中的信息可能已经有所发展或是发生改变。

go语言中int类型和string类型都是属于基本数据类型

两种类型的转化都非常简单

下面为大家提供两种int类型转化成string类型的方法!

go语言的类型转化都在strconv package里面,详情请参考:

http://golang.org/pkg/strconv

下面附上转化代码:

package main

import (
	"fmt"
	"strconv"
)

var i int = 10

func main() {
	// 通过Itoa方法转换
	str1 := strconv.Itoa(i)

	// 通过Sprintf方法转换
	str2 := fmt.Sprintf("%d", i)

	// 打印str1
	fmt.Println(str1)
	// 打印str2
	fmt.Println(str2)
}

%d代表Integer

详细说明请参考:http://golang.org/pkg/fmt/


有疑问加站长微信联系(非本文作者)

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK