如何在Go中将int值转换为字符串?

huangapple go评论97阅读模式
英文:

How to convert an int value to string in Go?

问题

s := strconv.Itoa(i)

To concatenate two strings in Go, you can use the + operator:

s := "ab" + "c"

英文:
i := 123
s := string(i) 

s is 'E', but what I want is "123"

Please tell me how can I get "123".

And in Java, I can do in this way:

String s = "ab" + "c"  // s is "abc"

how can I concat two strings in Go?

答案1

得分: 1157

使用strconv包的Itoa函数。

例如:

package main

import (
    "strconv"
    "fmt"
)

func main() {
    t := strconv.Itoa(123)
    fmt.Println(t)
}

您可以通过使用+连接字符串,或者使用strings包的Join函数来连接字符串。

英文:

Use the strconv package's Itoa function.

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.

答案2

得分: 197

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

如果你知道value的具体类型,可以使用相应的格式化符号,例如%d表示int类型

更多信息 - fmt

英文:
fmt.Sprintf("%v",value);

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

More info - fmt

答案3

得分: 91

fmt.Sprintfstrconv.Itoastrconv.FormatInt可以完成这个任务。但是Sprintf会使用reflect包,并且会分配一个额外的对象,所以它不是一个高效的选择。

英文:

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

如何在Go中将int值转换为字符串?

答案4

得分: 76

值得注意的是,strconv.Itoastrconv.FormatInt(i int64, base int) string的简写形式,其中base为10。

例如:

strconv.Itoa(123)

等同于

strconv.FormatInt(int64(123), 10)
英文:

It is interesting to note that strconv.Itoa is shorthand for

func FormatInt(i int64, base int) string

with base 10

For Example:

strconv.Itoa(123)

is equivalent to

strconv.FormatInt(int64(123), 10)

答案5

得分: 46

你可以使用fmt.Sprintf或者strconv.FormatFloat

例如

package main

import (
	"fmt"
)

func main() {
	val := 14.7
	s := fmt.Sprintf("%f", val)
	fmt.Println(s)
}
英文:

You can use fmt.Sprintf or strconv.FormatFloat

For example

package main

import (
	"fmt"
)

func main() {
	val := 14.7
	s := fmt.Sprintf("%f", val)
	fmt.Println(s)
}

答案6

得分: 34

在这种情况下,strconvfmt.Sprintf都可以完成相同的工作,但是使用strconv包的Itoa函数是最好的选择,因为在转换过程中fmt.Sprintf会分配一个额外的对象。

查看两者的基准测试结果:https://gist.github.com/evalphobia/caee1602969a640a4530

示例代码请参考:https://play.golang.org/p/hlaz_rMa0D

英文:

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.

如何在Go中将int值转换为字符串?
check the benchmark here: https://gist.github.com/evalphobia/caee1602969a640a4530

see https://play.golang.org/p/hlaz_rMa0D for example.

答案7

得分: 14

int64转换为字符串:

n := int64(32)
str := strconv.FormatInt(n, 10)

fmt.Println(str)
// 输出“32”
英文:

Converting int64:

n := int64(32)
str := strconv.FormatInt(n, 10)

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

答案8

得分: 11

另一个选项:

package main
import "fmt"

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

https://golang.org/pkg/fmt#Sprint

英文:

Another option:

package main
import "fmt"

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

https://golang.org/pkg/fmt#Sprint

答案9

得分: 8

// ToString 将参数转换为字符串
func ToString(arg interface{}, timeFormat ...string) string {
if len(timeFormat) > 1 {
log.SetFlags(log.Llongfile | log.LstdFlags)
log.Println(errors.New(fmt.Sprintf("timeFormat的长度应为1")))
}
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 ""
}
}

英文:

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 ""
	}
}

答案10

得分: 4

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" 
"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)
}

huangapple
  • 本文由 发表于 2012年4月11日 20:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/10105935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定