以下代码
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, ","))
}
产生这个错误:
./test.go:17: cannot use Foos (type []Foo) as type []string in argument to strings.Join
这是有道理的,因为 Foo 不是字符串,而是从字符串派生的。有没有办法在不复制的情况下将“[]Foo”强制为“[]string”?
相关分类