`fmt`包的函数是否支持数组的格式化器?

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

Do functions of the `fmt` package support formatters for arrays?

问题

我正在尝试使用多个参数提交电子邮件,并且我有一个单独的文件中包含一些打印动词的电子邮件,但是由于动词太多,我最终得到了这样一行代码:

message := fmt.Sprintf(util.CONTACT_EMAIL, form.Name, form.Email, form.Email, form.Phone, form.Phone, form.Message, ...)

这看起来很糟糕。我重复一些动词的原因是为了获取href,例如<a href="mailto:%s">%s</a>等等。如果有人有更好的方法,我真的很想知道。

但是接下来是我的问题.. Go语言是否有类似于PHP中的vsprintf的格式化程序?它基本上接受一个数组作为参数,就像这样:

string vsprintf(string $format , array $args) 

..而不是我上面那个混乱的代码,这样可以使代码更易读。

我查看了文档,但似乎没有找到类似的内容..但是Go语言的很多东西对我来说仍然很陌生,所以也许我忽略了它。

英文:

I am trying to submit an email with multiple paramaters and I have emails in a separate file with some printing verbs, but since there are so many verbs I end up with a line like this:

message := fmt.Sprintf(util.CONTACT_EMAIL, form.Name, form.Email, form.Email,     form.Phone, form.Phone, form.Message, ...)

and it just goes on and on which looks bad. And the reason I repeat some verbs is to get the href's, for example &lt;a href&quot;mailto:%s&quot;&gt;%s&lt;/a&gt;, and so forth. If anyone has a better approach to that I'd really like to know.

But on to my question.. Does Go have a formatter that works similar to
vsprintf in PHP? It basically takes an array as the arguments so it would be like:

string vsprintf(string $format , array $args) 

..instead of the mess I have above, which allows it to be a bit more readable.

I looked on the docs but don't seem to see anything..but a lot of what Go does is still foreign to me so maybe I overlooked it.

答案1

得分: 1

如果你只想将一部分参数传递给fmt.Sprintf(或任何其他接受可变数量参数的函数),你可以这样做:

func main() {
    s := []interface{}{1, 2, 5}
    z := fmt.Sprintf("%d, %d, %d", s...)
    print(z)
}

如果你有一个除了空接口之外的任何类型的切片,我们需要将其复制到一个空接口的切片中:

func main() {
    s := []int{1, 2, 5}
    
    // 我们需要逐个将每个元素复制到 []interface{} 中,
    // 因为它们在内存中的布局在 Go 中是不同的。
    var x []interface{}
    for _, i := range s {
        x = append(x, i)
    }

    // 将新切片的内容传递给 sprintf
    z := fmt.Sprintf("%d, %d, %d", x...)
    print(z)
}
英文:

If you just want to pass a slice of arguments to fmt.Sprintf (or any other function that takes a variadic number of arguments) you can do that like this:

func main() {
	s := []interface{}{1,2,5}
	z := fmt.Sprintf(&quot;%d, %d, %d&quot;, s...)
	print(z)
}

or if you have a slice of anything that isn't the empty interface, we have to copy it into a slice of the empty interface:

func main() {
	s := []int{1,2,5}
	
    // we need to copy each element one-by-one into a []interface{} 
    // because they are laid out differently in memory in go.
	var x []interface{}
	for _, i := range s {
		x = append(x, i)
	}

    // pass the contents of the new slice into sprintf
	z := fmt.Sprintf(&quot;%d, %d, %d&quot;, x...)
	print(z)
}

huangapple
  • 本文由 发表于 2016年1月23日 01:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/34952479.html
匿名

发表评论

匿名网友

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

确定