s is ‘E’, maar wat ik wil is “123”
Vertel me hoe ik “123” kan krijgen.
En in Java kan ik op deze manier doen:
String s = "ab" + "c" // s is "abc"
hoe kan ik twee strings concat
in Go?
Antwoord 1, autoriteit 100%
Gebruik het strconv
pakket Itoa
functie.
Bijvoorbeeld:
package main
import (
"strconv"
"fmt"
)
func main() {
t := strconv.Itoa(123)
fmt.Println(t)
}
Je kunt strings eenvoudig samenvoegen door ze te +
in te voeren, of door de functie Join
te gebruiken van de strings
pakket.
Antwoord 2, autoriteit 18%
fmt.Sprintf("%v",value);
Als u het specifieke type waarde kent, gebruikt u de bijbehorende opmaak, bijvoorbeeld %d
voor int
Meer info – fmt
Antwoord 3, autoriteit 6%
Het is interessant op te merken dat strconv.Itoa
afkortingvoor
func FormatInt(i int64, base int) string
met grondtal 10
Bijvoorbeeld:
strconv.Itoa(123)
is gelijk aan
strconv.FormatInt(int64(123), 10)
Antwoord 4, autoriteit 6%
fmt.Sprintf
, strconv.Itoa
en strconv.FormatInt
zullen het werk doen. Maar Sprintf
zal het pakket reflect
gebruiken, en het zal nog een object toewijzen, dus het is geen efficiënte keuze.
Antwoord 5, autoriteit 4%
Je kunt fmt.Sprintfof strconv.FormatFloat
Bijvoorbeeld
package main
import (
"fmt"
)
func main() {
val := 14.7
s := fmt.Sprintf("%f", val)
fmt.Println(s)
}
Antwoord 6, autoriteit 3%
In dit geval doen zowel strconv
als fmt.Sprintf
hetzelfde werk, maar met het strconv
pakket Itoa
functie is de beste keuze, omdat fmt.Sprintf
nog een object toewijst tijdens de conversie.
bekijk de benchmark hier: https://gist.github.com/evalphobia/caee1602969a640a4530
zie bijvoorbeeld https://play.golang.org/p/hlaz_rMa0D.
Antwoord 7
int64
converteren:
n := int64(32)
str := strconv.FormatInt(n, 10)
fmt.Println(str)
// Prints "32"
Antwoord 8
ok, de meeste hebben je iets goeds laten zien.
Laat me je dit geven:
// 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 ""
}
}
Antwoord 9
Een andere optie:
package main
import "fmt"
func main() {
n := 123
s := fmt.Sprint(n)
fmt.Println(s == "123")
}
https://golang.org/pkg/fmt#Sprint
Antwoord 10
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)
}