string 如何在 Go 中将 int 值转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10105935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert an int value to string in Go?
提问by hardPass
i := 123
s := string(i)
s is 'E', but what I want is "123"
s 是“E”,但我想要的是“123”
Please tell me how can I get "123".
请告诉我如何获得“123”。
And in Java, I can do in this way:
在 Java 中,我可以这样做:
String s = "ab" + "c" // s is "abc"
how can I concat
two strings in Go?
如何concat
在 Go 中使用两个字符串?
回答by Klaus Byskov Pedersen
Use the strconv
package's Itoa
function.
使用strconv
包的Itoa
功能。
For example:
例如:
package main
import (
"strconv"
"fmt"
)
func main() {
t := strconv.Itoa(123)
fmt.Println(t)
}
You can concat strings simply by +
'ing them, or by using the Join
function of the strings
package.
您可以简单地通过+
'ing 或使用包的Join
函数来连接字符串strings
。
回答by Jasmeet Singh
回答by kgthegreat
回答by Bryce
回答by lazy1
回答by manigandand
In this case both strconv
and fmt.Sprintf
do the same job but using the strconv
package's Itoa
function is the best choice, because fmt.Sprintf
allocate one more object during conversion.
在这种情况下,两个strconv
与fmt.Sprintf
做同样的工作,但使用该strconv
软件包的Itoa
功能是最好的选择,因为fmt.Sprintf
在转换过程中分配一个多个对象。
check the benchmark here: https://gist.github.com/evalphobia/caee1602969a640a4530
在此处检查基准:https: //gist.github.com/evalphobia/caee1602969a640a4530
see https://play.golang.org/p/hlaz_rMa0Dfor example.
回答by Cae Vecchi
Converting int64
:
转换int64
:
n := int64(32)
str := strconv.FormatInt(n, 10)
fmt.Println(str)
// Prints "32"
回答by fwhez
ok,most of them have shown you something good. Let'me give you this:
好的,他们中的大多数都向你展示了一些好东西。让我给你这个:
// ToString Change arg to string
func ToString(arg interface{}, timeFormat ...string) string {
if len(timeFormat) > 1 {
log.SetFlags(log.Llongfile | log.LstdFlags)
log.Println(errors.New(fmt.Sprintf("timeFormat's length should be one")))
}
var tmp = reflect.Indirect(reflect.ValueOf(arg)).Interface()
switch v := tmp.(type) {
case int:
return strconv.Itoa(v)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case string:
return v
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case time.Time:
if len(timeFormat) == 1 {
return v.Format(timeFormat[0])
}
return v.Format("2006-01-02 15:04:05")
case jsoncrack.Time:
if len(timeFormat) == 1 {
return v.Time().Format(timeFormat[0])
}
return v.Time().Format("2006-01-02 15:04:05")
case fmt.Stringer:
return v.String()
case reflect.Value:
return ToString(v.Interface(), timeFormat...)
default:
return ""
}
}
回答by Sagiruddin Mondal
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)
}