mixing “exploded” slices and regular parameters in variadic functions

后端 未结 3 806
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 02:58

I\'m wondering why it\'s not possible to do the following in go:

func main() {
    stuff := []string{\"baz\", \"bla\"}
    foo(\"bar\", stuff...)
}

func foo         


        
3条回答
  •  孤城傲影
    2020-12-30 03:21

    The ugly way to get this to work is make it into a new variadic.

    foo(append([]string{"bar"}, stuff...)...)
    

    And if the order doesn't matter:

    foo(append(stuff, "bar")...)
    

    https://play.golang.org/p/mY6y0vScfPB

提交回复
热议问题