i := 123
s := string(i)
s คือ 'E' แต่สิ่งที่ฉันต้องการคือ "123"
กรุณาบอกฉันว่าฉันจะได้รับ "123"
และใน Java ฉันสามารถทำได้ด้วยวิธีนี้:
String s = "ab" + "c" // s is "abc"
ฉันจะconcatสองสายใน Go ได้อย่างไร?
i := 123
s := string(i)
s คือ 'E' แต่สิ่งที่ฉันต้องการคือ "123"
กรุณาบอกฉันว่าฉันจะได้รับ "123"
และใน Java ฉันสามารถทำได้ด้วยวิธีนี้:
String s = "ab" + "c" // s is "abc"
ฉันจะconcatสองสายใน Go ได้อย่างไร?
คำตอบ:
ใช้ฟังก์ชั่นstrconvของแพ็คเกจItoa
ตัวอย่างเช่น:
package main
import (
"strconv"
"fmt"
)
func main() {
t := strconv.Itoa(123)
fmt.Println(t)
}
คุณสามารถเชื่อมสตริงได้ง่ายๆโดย+'ไอเอ็นจีพวกเขาหรือโดยใช้Joinฟังก์ชั่นของstringsแพคเกจ
fmt.Sprintf("%v",value);
ถ้าคุณรู้ว่าประเภทที่เฉพาะเจาะจงของค่าใช้จัดรูปแบบที่สอดคล้องกันตัวอย่างเช่น%dสำหรับint
ข้อมูลเพิ่มเติม - fmt
%dสำหรับ int - สิ่งนี้
เป็นที่น่าสนใจที่จะทราบว่าstrconv.Itoaเป็นชวเลขสำหรับ
func FormatInt(i int64, base int) string
พร้อมฐาน 10
ตัวอย่างเช่น:
strconv.Itoa(123)
เทียบเท่ากับ
strconv.FormatInt(int64(123), 10)
fmt.Sprintf, strconv.Itoaและstrconv.FormatIntจะทำงาน แต่Sprintfจะใช้แพคเกจreflectและจะจัดสรรอีกหนึ่งวัตถุดังนั้นจึงไม่ใช่ตัวเลือกที่มีประสิทธิภาพ
คุณสามารถใช้fmt.Sprintf
ดูhttp://play.golang.org/p/bXb1vjYbycเป็นตัวอย่าง
ในกรณีนี้ทั้งสองstrconvและfmt.Sprintfทำงานเดียวกัน แต่ใช้ฟังก์ชั่นstrconvของแพคเกจItoaเป็นตัวเลือกที่ดีที่สุดเพราะfmt.Sprintfจัดสรรอีกหนึ่งวัตถุระหว่างการแปลง
ตรวจสอบมาตรฐานที่นี่: https://gist.github.com/evalphobia/caee1602969a640a4530
ดูhttps://play.golang.org/p/hlaz_rMa0Dตัวอย่างเช่น
fmt.Sprintfและstrconv.iotaมีความคล้ายคลึงกันในแง่ของการใช้งานง่ายและข้อมูลข้างต้นแสดงให้เห็นว่าเล็กน้อยจะเร็วขึ้นโดยมีผลกระทบ GC ต่ำกว่าจึงปรากฏว่าiotaควรใช้โดยทั่วไปเมื่อจำนวนเต็มเดียวต้องแปลง
การแปลงint64:
n := int64(32)
str := strconv.FormatInt(n, 10)
fmt.Println(str)
// Prints "32"
ตกลงส่วนใหญ่แสดงให้คุณเห็นสิ่งที่ดี เราให้สิ่งนี้กับคุณ:
// 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)
}