Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在Go中将整型值转换为字符串?_String_Go_Int_Converters - Fatal编程技术网

String 如何在Go中将整型值转换为字符串?

String 如何在Go中将整型值转换为字符串?,string,go,int,converters,String,Go,Int,Converters,s是“E”,但我要的是“123” 请告诉我怎样才能得到“123” 在Java中,我可以这样做: i := 123 s := string(i) 如何在Go中使用两个字符串?使用软件包的Itoa功能 例如: String s = "ab" + "c" // s is "abc" 您可以通过+简单地压缩字符串,或者使用包的Join函数来压缩字符串。您可以使用或 比如说 package main import ( "strconv" "fmt" ) func main()

s是“E”,但我要的是“123”

请告诉我怎样才能得到“123”

在Java中,我可以这样做:

i := 123
s := string(i) 
如何在Go中使用两个字符串?

使用软件包的
Itoa
功能

例如:

String s = "ab" + "c"  // s is "abc"
您可以通过
+
简单地压缩字符串,或者使用包的
Join
函数来压缩字符串。

您可以使用或

比如说

package main

import (
    "strconv"
    "fmt"
)

func main() {
    t := strconv.Itoa(123)
    fmt.Println(t)
}
如果您知道特定类型的值,请使用相应的格式化程序,例如
%d
用于
int


更多信息-

值得注意的是,
strconv.Itoa

以10为基数

例如:

func FormatInt(i int64, base int) string
相当于

strconv.Itoa(123)

fmt.Sprintf
strconv.Itoa
strconv.FormatInt
将完成这项工作。但是
Sprintf
将使用包
reflect
,并且它将分配一个以上的对象,因此这不是一个有效的选择


在这种情况下,
strconv
fmt.Sprintf
都做同样的工作,但是使用
strconv
包的
Itoa
函数是最佳选择,因为
fmt.Sprintf
在转换过程中分配一个以上的对象

在此处检查基准:


参见示例。

转换
int64

strconv.FormatInt(int64(123), 10)

好的,他们中的大多数都给你展示了一些好东西。 让我给你这个:

//将参数更改为字符串
func ToString(参数接口{},时间格式…字符串)字符串{
如果len(timeFormat)>1{
log.SetFlags(log.Llongfile | log.LstdFlags)
log.Println(errors.New(fmt.Sprintf(“timeFormat的长度应该是一”))
}
var tmp=reflect.Indirect(reflect.ValueOf(arg)).Interface()
开关v:=tmp(类型){
案例int:
返回strconv.Itoa(v)
案例8:
返回strconv.FormatInt(int64(v),10)
案例16:
返回strconv.FormatInt(int64(v),10)
案例int32:
返回strconv.FormatInt(int64(v),10)
案例int64:
返回strconv.FormatInt(v,10)
大小写字符串:
返回v
案例32:
返回strconv.FormatFloat(float64(v),'f',-1,32)
案例64:
返回strconv.FormatFloat(v,'f',-1,64)
案件时间。时间:
如果len(timeFormat)=1{
return v.Format(timeFormat[0])
}
返回v.格式(“2006-01-02 15:04:05”)
案例JSonTrack.时间:
如果len(timeFormat)=1{
返回v.Time().Format(timeFormat[0])
}
return v.Time()格式(“2006-01-02 15:04:05”)
案例模板纵梁:
return v.String()
案例。价值:
返回到字符串(v.Interface(),timeFormat…)
违约:
返回“”
}
}
另一个选项:

package main

import (
    "fmt" 
    "strconv"
)

func main(){
//First question: how to get int string?

    intValue := 123
    // keeping it in separate variable : 
    strValue := strconv.Itoa(intValue) 
    fmt.Println(strValue)

//Second question: how to concat two strings?

    firstStr := "ab"
    secondStr := "c"
    s := firstStr + secondStr
    fmt.Println(s)
}

为什么语言设计者认为像“Itoa”这样的神秘函数名称比更具描述性的名称更可取?@luke它来自C语言遗产,整个机器可能有256K内存;为了适应更多的功能,牺牲了可用性。Go的创造者都深深地植根于这一传统之中,对这些名字感到非常舒服。在我看来,将历史置于可访问性和易学性之上是一种糟糕的设计:(@Luke取决于你的目标用户是谁以及约定的强度。一些UI仍然有一张软盘作为保存图标:)为了便于记忆int-this的名称ItoA-Integer到ASCII
%d
,我不建议使用这种方法,因为这种方法的效率远远低于其他转换方法,因为它使用反射。很棒的注释@RicardoSouza。我在这里引用了你的话。@Boon对你的应用程序有明显的影响吗?一如往常——这要看情况而定。另一个对象意味着,除了明显的临时小内存命中之外,还有一个对象需要被垃圾收集。如果您以高速率调用该函数,例如,作为从某处接收消息时使用的某些序列化过程的一部分,它可能会产生重大影响。由于
fmt.Sprintf
strconv.iota
在易用性方面相似,且上述数据显示iota速度更快,GC影响更小,因此,当单个整数需要转换时,通常应使用
iota
。我这么快就开始考虑这一级别的优化似乎为时过早。最好是先写可读的代码。@Boon它们同样可读。最好用快一点的。另外,如果一个新的Golang程序员不是从做大量转换的东西开始,那该怎么说呢?我是一个有经验的程序员,现在正在编写我的第一个Golang代码,我正处于这种情况。有趣的是,根据第二个问题(字符串连接)的基准测试,直接调用FormatInt()而不是Itoa()可以节省0.1纳秒,这涵盖了效率。
strconv.Itoa(I)
(int到ASCII)将int设置为字符串。看
strconv.Atoi
(ASCII到int)将字符串设置为int。请参阅。
strconv.FormatInt(int64(123), 10)
n := int64(32)
str := strconv.FormatInt(n, 10)

fmt.Println(str)
// Prints "32"
package main

import (
    "fmt" 
    "strconv"
)

func main(){
//First question: how to get int string?

    intValue := 123
    // keeping it in separate variable : 
    strValue := strconv.Itoa(intValue) 
    fmt.Println(strValue)

//Second question: how to concat two strings?

    firstStr := "ab"
    secondStr := "c"
    s := firstStr + secondStr
    fmt.Println(s)
}
package main
import "fmt"

func main() {
   n := 123
   s := fmt.Sprint(n)
   fmt.Println(s == "123")
}