在Go中,什么是将字符串类型数组转换为字符串数组的最佳方法?

The following code

package main

import "strings"
import "fmt"

type Foo string

const (
    Bar    Foo = "bar"
    Snafu      = "snafu"
    Foobar     = "foobar"
)

var Foos = []Foo{Bar, Snafu, Foobar}

func main() {
    fmt.Println("Foos: " + strings.Join(Foos, ","))
}

Produces this error:

./test.go:17: cannot use Foos (type []Foo) as type []string in argument to strings.Join

This makes sense since Foo is not string but it is derived from string. Is there any way to coerce the "[]Foo" to "[]string" without copying?

Pretty much the best and only way to do it is to loop over the slice of Foo objects as in (causing a copy):

func main() {
    results := make([]string, 0)
    for _, r := range Foos{
       results = append(results, string(r))
    }
    fmt.Println("Foos: " + strings.Join(results, ","))
}

Unfortunately as of this time Go does not support the concept type coercion and requires a copy. As an alternative you can type alias []Foo and add a Stringer method to it therefore encapsulating such details as in: http://play.golang.org/p/FuOoLNrCvD

See the following for a similar question: Cannot convert []string to []interface {}