How to merge multiple strings and int into a single string

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

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,只是它返回一个字符串。下面是一个示例:

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:

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.

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

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

strings.Join vs fmt.Sprintf

BenchmarkFmt-4  	 2000000	       685 ns/op
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

你可以简单地这样做:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	result := "str1" + "str2" + strconv.Itoa(123) + "str3" + strconv.Itoa(12)
	fmt.Println(result)
}

// 使用 fmt.Sprintf()
var s1 = "abc"
var s2 = "def"
var num = 100
ans := fmt.Sprintf("%s%d%s", s1, num, s2)
fmt.Println(ans)

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

英文:

You can simply do this:

package main

    import (
    	"fmt" 
    	"strconv"
    )
    
    func main() {

         
    	 result:="str1"+"str2"+strconv.Itoa(123)+"str3"+strconv.Itoa(12)
    	 fmt.Println(result)
         
    }

Using fmt.Sprintf()

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

答案4

得分: -1

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

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

package main

import (
   "text/template"
   "strings"
)

func format(s string, v interface{}) string {
   t, b := new(template.Template), new(strings.Builder)
   template.Must(t.Parse(s)).Execute(b, v)
   return b.String()
}

func main() {
   s := struct{
      Key string
      Key2 int
   }{"value", 100}
   f := format("key:{{.Key}}, key2:{{.Key2}}", s)
   println(f)
}

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

package main
import "fmt"

func main() {
   s := fmt.Sprint("key:", "value", ", key2:", 100)
   println(s)
}

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

英文:

You can use text/template:

package main

import (
   "text/template"
   "strings"
)

func format(s string, v interface{}) string {
   t, b := new(template.Template), new(strings.Builder)
   template.Must(t.Parse(s)).Execute(b, v)
   return b.String()
}

func main() {
   s := struct{
      Key string
      Key2 int
   }{"value", 100}
   f := format("key:{{.Key}}, key2:{{.Key2}}", s)
   println(f)
}

or fmt.Sprint:

package main
import "fmt"

func main() {
   s := fmt.Sprint("key:", "value", ", key2:", 100)
   println(s)
}

答案5

得分: -1

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

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	const name, age = "John", 26
	s := fmt.Sprintf("%s is %d years old.\n", name, age)
	io.WriteString(os.Stdout, s) // 为简单起见,忽略错误。
}

输出:

John is 26 years old.

英文:

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

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	const name, age = "John", 26
	s := fmt.Sprintf("%s is %d years old.\n", name, age)
	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}

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:

确定