i := 123
s := string(i)
s è 'E', ma quello che voglio è "123"
Ti prego, dimmi come posso ottenere "123".
E in Java, posso fare in questo modo:
String s = "ab" + "c" // s is "abc"
come posso concatdue stringhe in Go?
i := 123
s := string(i)
s è 'E', ma quello che voglio è "123"
Ti prego, dimmi come posso ottenere "123".
E in Java, posso fare in questo modo:
String s = "ab" + "c" // s is "abc"
come posso concatdue stringhe in Go?
Risposte:
Usa la funzione strconvdel pacchetto Itoa.
Per esempio:
package main
import (
"strconv"
"fmt"
)
func main() {
t := strconv.Itoa(123)
fmt.Println(t)
}
Puoi concatenare le stringhe semplicemente +inserendole o usando la Joinfunzione del stringspacchetto.
fmt.Sprintf("%v",value);
Se si conosce il tipo specifico di valore utilizzare il formattatore corrispondente, ad esempio %dperint
Maggiori informazioni - fmt
%dper int - this
È interessante notare che strconv.Itoaè una scorciatoia per
func FormatInt(i int64, base int) string
con base 10
Per esempio:
strconv.Itoa(123)
è equivalente a
strconv.FormatInt(int64(123), 10)
Puoi usare fmt.Sprintf
Vedi http://play.golang.org/p/bXb1vjYbyc per esempio.
In questo caso entrambi strconve fmt.Sprintffanno lo stesso lavoro ma usare la funzione strconvdel pacchetto Itoaè la scelta migliore, perché fmt.Sprintfallocare un altro oggetto durante la conversione.
controlla qui il benchmark: https://gist.github.com/evalphobia/caee1602969a640a4530
vedi ad esempio https://play.golang.org/p/hlaz_rMa0D .
fmt.Sprintfe strconv.iotasono simili in termini di facilità d'uso e i dati sopra riportati mostrano che iota è più veloce con un impatto GC inferiore, sembra che iotadovrebbe essere usato in generale quando un singolo numero intero deve essere convertito.
Conversione int64:
n := int64(32)
str := strconv.FormatInt(n, 10)
fmt.Println(str)
// Prints "32"
ok, molti di loro ti hanno mostrato qualcosa di buono. Lascia che ti dia questo:
// 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 ""
}
}
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)
}