Cómo convertir un valor int a cadena en Go?


i := 123
s := string(i) 

S es 'E', pero lo que quiero es "123"

Por favor dime cómo puedo obtener "123".

Y en Java, puedo hacer de esta manera:

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

¿Cómo puedo concat dos cadenas en Go?

Author: hardPass, 2012-04-11

7 answers

Utilice el strconv función Itoa del paquete.

Por ejemplo:

package main

import (
    "strconv"
    "fmt"
)

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

Usted puede concat cadenas simplemente por +'ing ellos, o mediante el uso de la función Join de la strings paquete.

 514
Author: Klaus Byskov Pedersen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-11 12:39:11
fmt.Sprintf("%v",value);

Si conoce el tipo específico de valor use el formateador correspondiente por ejemplo %d para int

Más información - fmt

 101
Author: Jasmeet Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-01 21:58:47
 33
Author: lazy1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-11 21:26:31

Es interesante notar que strconv.Itoa es abreviatura para

func FormatInt(i int64, base int) string

Con base 10

Por Ejemplo:

strconv.Itoa(123)

Es equivalente a

strconv.FormatInt(int64(123), 10)
 32
Author: kgthegreat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-30 02:06:49

fmt.Sprintf, strconv.Itoa y strconv.FormatInt hará el trabajo. Pero Sprintf utilizará el paquete reflect, y asignará un objeto más, por lo que no es una buena opción.

introduzca la descripción de la imagen aquí

 26
Author: Bryce,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-31 10:04:07

En este caso, tanto strconv como fmt.Sprintf hacen el mismo trabajo, pero usar la función strconv del paquete Itoa es la mejor opción, porque fmt.Sprintf asigna un objeto más durante la conversión.

compruebe el resultado de nenchmark de ambos compruebe el punto de referencia aquí: https://gist.github.com/evalphobia/caee1602969a640a4530

Véase https://play.golang.org/p/hlaz_rMa0D por ejemplo.

 14
Author: manigandand,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-28 13:24:42

Convirtiendo int64:

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

fmt.Println(str)
// Prints "32"
 0
Author: Cae Vecchi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-03 11:17:20