fmt.Sprintf传递一个参数数组

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

fmt.Sprintf passing an array of arguments

问题

抱歉问一个基础的问题。我想将一个切片作为参数传递给fmt.Sprintf。类似这样:

values := []string{"foo", "bar", "baz"}
result := fmt.Sprintf("%s%s%s", values...)

结果应该是foobarbaz,但显然这样是行不通的。

(我想格式化的字符串比这个复杂,所以简单的拼接不行:)

所以问题是:如果我有一个数组,我如何将它作为单独的参数传递给fmt.Sprintf?或者:在Go中,我可以调用一个函数并传递一个参数列表吗?

英文:

Sorry for the basic question. I'd like to pass a slice as arguments to fmt.Sprintf. Something like this:

values := []string{"foo", "bar", "baz"}
result := fmt.Sprintf("%s%s%s", values...)

And the result would be foobarbaz, but this obviously doesn't work.

(the string I want to format is more complicated than that, so a simple concatenation won't do it fmt.Sprintf传递一个参数数组

So the question is: if I have am array, how can I pass it as separated arguments to fmt.Sprintf? Or: can I call a function passing an list of arguments in Go?

答案1

得分: 26

你在IRC上发现,这将起作用:

values := []interface{}{"foo", "bar", "baz"}
result := fmt.Sprintf("%s%s%s", values...)

你的原始代码无法工作,因为fmt.Sprintf接受一个[]interface{},而[]string无法隐式或显式地转换为该类型。

英文:

As you found out on IRC, this will work:

values := []interface{}{"foo", "bar", "baz"}
result := fmt.Sprintf("%s%s%s", values...)

Your original code doesn't work because fmt.Sprintf accepts a []interface{} and []string can't be converted to that type, implicitly or explicitly.

答案2

得分: 4

如果格式严格符合您的要求,那么接受的答案确实是正确的方法。

如果只是为了记录目的,您只想打印它们,那么另一种方法是使用%v

values := []string{"foo", "bar", "baz"}
result := fmt.Sprintf("%v", values)
fmt.Println(result)

结果:

[foo bar baz]

Go playground: https://go.dev/play/p/_fZrT9mMmdb

英文:

If the format is strictly like what you asked, then the accepted answer is indeed the way to go.

Fyi, if it is for logging purposes where you simply want to print them, then another way is by using %v

values := []string{"foo", "bar", "baz"}
result := fmt.Sprintf("%v", values)
fmt.Println(result)

result:

[foo bar baz]

Go playground: https://go.dev/play/p/_fZrT9mMmdb

答案3

得分: 0

如果不使用数组,使用args ...any

package main

import "fmt"

func main() {
    format := "%d,%d: %s"

    check(format, 4, 5, "hello")
}

func check(format string, args ...any) {

    fmt.Printf(format, args...)
}
英文:

if not use array, use args ...any

package main

import "fmt"

func main() {
    format := "%d,%d: %s"

    check(format, 4, 5, "hello")
}

func check(format string, args ...any) {

    fmt.Printf(format, args...)
}

答案4

得分: -1

我认为这样做的问题在于Sprintf无法处理长度不受限制的切片,所以这不实际。格式参数的数量必须与格式指令的数量相匹配。你要么将它们提取到本地变量中,要么编写一个循环遍历切片并将字符串连接在一起的代码。我会选择后者。

英文:

I think the issue with doing this is that the Sprintf won't work with unbounded length slices, so it's not practical. The number of format parameters must match the number of formatting directives. You will either have to extract them into local variables or write something to iterate the slice and concatenate the strings together. I'd go for the latter.

huangapple
  • 本文由 发表于 2011年8月22日 18:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/7145905.html
匿名

发表评论

匿名网友

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

确定