How to merge multiple strings and int into a single string

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

How to merge multiple strings and int into a single string

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Go的新手。我找不到任何官方文档显示如何将多个字符串合并为一个新的字符串。

我的期望是:

输入"key:""value"", key2:"100

输出"Key:value, key2:100"

如果可能的话,我想使用+来像Java和Swift一样合并字符串。

英文:

I am a newbie in Go. I can't find any official docs showing how to merge multiple strings into a new string.

What I'm expecting:

Input: "key:", "value", ", key2:", 100

Output: "Key:value, key2:100"

I want to use + to merge strings like in Java and Swift if possible.

答案1

得分: 84

我喜欢使用fmt的Sprintf方法来处理这种情况。它的用法类似于Go或C中的Printf,只是它返回一个字符串。下面是一个示例:

  1. output := fmt.Sprintf("%s%s%s%d", "key:", "value", ", key2:", 100)

关于fmt.Sprintf的Go文档。

英文:

I like to use fmt's Sprintf method for this type of thing. It works like Printf in Go or C only it returns a string. Here's an example:

  1. output := fmt.Sprintf("%s%s%s%d", "key:", "value", ", key2:", 100)

Go docs for fmt.Sprintf

答案2

得分: 26

你可以使用strings.Join,它的速度几乎比fmt.Sprintf快3倍。然而,它可能不太易读。

output := strings.Join([]string{"key:", "value", ", key2:", strconv.Itoa(100)}, "")

参考链接:https://play.golang.org/p/AqiLz3oRVq

strings.Join vs fmt.Sprintf

BenchmarkFmt-4 2000000 685 ns/op
BenchmarkJoins-4 5000000 244 ns/op

Buffer

如果你需要合并大量的字符串,我建议使用缓冲区而不是上述提到的解决方案。

英文:

You can use strings.Join, which is almost 3x faster than fmt.Sprintf. However it can be less readable.

  1. output := strings.Join([]string{"key:", "value", ", key2:", strconv.Itoa(100)}, "")

See https://play.golang.org/p/AqiLz3oRVq

strings.Join vs fmt.Sprintf

  1. BenchmarkFmt-4 2000000 685 ns/op
  2. BenchmarkJoins-4 5000000 244 ns/op

Buffer

If you need to merge a lot of strings, I'd consider using a buffer rather than those solutions mentioned above.

答案3

得分: 2

你可以简单地这样做:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. result := "str1" + "str2" + strconv.Itoa(123) + "str3" + strconv.Itoa(12)
  8. fmt.Println(result)
  9. }
  10. // 使用 fmt.Sprintf()
  11. var s1 = "abc"
  12. var s2 = "def"
  13. var num = 100
  14. ans := fmt.Sprintf("%s%d%s", s1, num, s2)
  15. fmt.Println(ans)

使用 fmt.Sprintf() 函数可以实现相同的效果。

英文:

You can simply do this:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. result:="str1"+"str2"+strconv.Itoa(123)+"str3"+strconv.Itoa(12)
  8. fmt.Println(result)
  9. }

Using fmt.Sprintf()

  1. var s1="abc"
  2. var s2="def"
  3. var num =100
  4. ans:=fmt.Sprintf("%s%d%s", s1,num,s2);
  5. fmt.Println(ans);

答案4

得分: -1

你可以使用text/template包或fmt.Sprint函数来进行格式化输出。

使用text/template包的示例代码如下:

  1. package main
  2. import (
  3. "text/template"
  4. "strings"
  5. )
  6. func format(s string, v interface{}) string {
  7. t, b := new(template.Template), new(strings.Builder)
  8. template.Must(t.Parse(s)).Execute(b, v)
  9. return b.String()
  10. }
  11. func main() {
  12. s := struct{
  13. Key string
  14. Key2 int
  15. }{"value", 100}
  16. f := format("key:{{.Key}}, key2:{{.Key2}}", s)
  17. println(f)
  18. }

使用fmt.Sprint函数的示例代码如下:

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := fmt.Sprint("key:", "value", ", key2:", 100)
  5. println(s)
  6. }

你可以参考以下链接获取更多信息:

英文:

You can use text/template:

  1. package main
  2. import (
  3. "text/template"
  4. "strings"
  5. )
  6. func format(s string, v interface{}) string {
  7. t, b := new(template.Template), new(strings.Builder)
  8. template.Must(t.Parse(s)).Execute(b, v)
  9. return b.String()
  10. }
  11. func main() {
  12. s := struct{
  13. Key string
  14. Key2 int
  15. }{"value", 100}
  16. f := format("key:{{.Key}}, key2:{{.Key2}}", s)
  17. println(f)
  18. }

or fmt.Sprint:

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := fmt.Sprint("key:", "value", ", key2:", 100)
  5. println(s)
  6. }

答案5

得分: -1

这是一个在Go语言中将字符串和整数组合的简单方法(版本:go1.18.1 最新版)

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. )
  7. func main() {
  8. const name, age = "John", 26
  9. s := fmt.Sprintf("%s is %d years old.\n", name, age)
  10. io.WriteString(os.Stdout, s) // 为简单起见,忽略错误。
  11. }

输出:

John is 26 years old.

英文:

Here's a simple way to combine string and integer in Go Lang(Version: go1.18.1 Latest)

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. )
  7. func main() {
  8. const name, age = "John", 26
  9. s := fmt.Sprintf("%s is %d years old.\n", name, age)
  10. io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
  11. }

Output:

John is 26 years old.

huangapple
  • 本文由 发表于 2016年2月25日 08:14:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/35615839.html
匿名

发表评论

匿名网友

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

确定