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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:26:42  来源:igfitidea点击:

How to convert an int value to string in Go?

stringgointconverters

提问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 concattwo strings in Go?

如何concat在 Go 中使用两个字符串?

回答by Klaus Byskov Pedersen

Use the strconvpackage's Itoafunction.

使用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 Joinfunction of the stringspackage.

您可以简单地通过+'ing 或使用包的Join函数来连接字符串strings

回答by Jasmeet Singh

fmt.Sprintf("%v",value);

If you know the specific type of value use the corresponding formatter for example %dfor int

如果你知道值的具体类型使用相应的格式,例如%d用于int

More info - fmt

更多信息 - fmt

回答by kgthegreat

It is interesting to note that strconv.Itoais shorthandfor

有趣的是,注意,strconv.Itoa速记

func FormatInt(i int64, base int) string

with base 10

以 10 为底

For Example:

例如:

strconv.Itoa(123)

is equivalent to

相当于

strconv.FormatInt(int64(123), 10)

回答by Bryce

fmt.Sprintf, strconv.Itoaand strconv.FormatIntwill do the job. But Sprintfwill use the package reflect, and it will allocate one more object, so it's not an efficient choice.

fmt.Sprintfstrconv.Itoa并且strconv.FormatInt将做的工作。但是Sprintf会使用 package reflect,它会再分配一个对象,所以这不是一个有效的选择。

enter image description here

在此处输入图片说明

回答by lazy1

回答by manigandand

In this case both strconvand fmt.Sprintfdo the same job but using the strconvpackage's Itoafunction is the best choice, because fmt.Sprintfallocate one more object during conversion.

在这种情况下,两个strconvfmt.Sprintf做同样的工作,但使用该strconv软件包的Itoa功能是最好的选择,因为fmt.Sprintf在转换过程中分配一个多个对象。

check the nenchmark result of bothcheck the benchmark here: https://gist.github.com/evalphobia/caee1602969a640a4530

check the nenchmark result of both在此处检查基准:https: //gist.github.com/evalphobia/caee1602969a640a4530

see https://play.golang.org/p/hlaz_rMa0Dfor example.

例如,请参见https://play.golang.org/p/hlaz_rMa0D

回答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)
}