将数组解压缩作为path的参数。

I want to unpack string array and pass to path.Join

package main

import (
    "fmt"
    "path"
)

func main() {
    p := []string{"a", "b", "c"}
    fmt.Println(path.Join(p...))
}

Output of this code is:

a/b/c

But if I pass arguments like:

package main

import (
    "fmt"
    "path"
)

func main() {
    p := []string{"a", "b", "c"}
    fmt.Println(path.Join("d", p...))
}

It doesn't work.

tmp/sandbox299218161/main.go:10: too many arguments in call to path.Join
    have (string, []string...)
    want (...string)

I think I have misunderstanding on unpacking, any suggestions?

You're not misunderstanding anything really, you just can't do that. The spec says:

If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.

In short, p... can only be used as the entire variadic part of the arguments, because when you do that, it simply re-uses p as the parameter slice within the function, instead of making a new one.

If you want to add some arguments at the beginning, you would have to construct your own slice with all of the arguments first, something like:

p := []string{"a", "b", "c"}
p2 := append([]string{"d"}, p...)
fmt.Println(path.Join(p2...))

which works alright and prints "d/a/b/c".