I'm trying to do something along these lines:
package main
import (
"fmt"
)
type StringWrap string
func main() {
s := []string{"a","b","c"}
sw := []StringWrap(s) //ERROR: cannot convert s (type []string) to type []StringWrap
fmt.Println(sw)
}
Am I doing something wrong? Or is this simply a limitation in go?
The Go Programming Language Specification
A type determines the set of values and operations specific to values of that type. A type may be specified by a (possibly qualified) type name or a type literal, which composes a new type from previously declared types.
Type = TypeName | TypeLit | "(" Type ")" . TypeName = identifier | QualifiedIdent . TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType | SliceType | MapType | ChannelType .
Named instances of the boolean, numeric, and string types are predeclared. Composite types—array, struct, pointer, function, interface, slice, map, and channel types—may be constructed using type literals.
Each type
T
has an underlying type: IfT
is a predeclared type or a type literal, the corresponding underlying type isT
itself. Otherwise,T
's underlying type is the underlying type of the type to whichT
refers in its type declaration.type T1 string type T2 T1 type T3 []T1 type T4 T3
The underlying type of
string
,T1
, andT2
isstring
. The underlying type of[]T1
,T3
, andT4
is[]T1
.Conversions are expressions of the form
T(x)
whereT
is a type andx
is an expression that can be converted to typeT
.A non-constant value
x
can be converted to typeT
in the case:x
's type andT
have identical underlying types.
For example,
package main
import "fmt"
type StringSliceWrap []string
func main() {
s := []string{"a", "b", "c"}
ssw := StringSliceWrap(s)
fmt.Println(ssw)
}
Output:
[a b c]