Go:将原始类型的切片转换为其别名的切片

huangapple go评论91阅读模式
英文:

Go: cast slice of primitive to a slice of its alias

问题

我正在尝试做类似这样的事情:

package main                                                                                                                                                                                                                                   
                  
import (          
    "fmt"         
)                 
                  
type StringWrap string
                  
func main() {     
    s := []string{"a","b","c"}
    sw := []StringWrap(s) //错误:无法将 s(类型为 []string)转换为类型 []StringWrap
    fmt.Println(sw)
}

我做错了什么吗?还是这只是Go语言的限制?

英文:

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?

答案1

得分: 2

> Go编程语言规范
>
> 类型
>
> 类型决定了特定类型的值和操作的集合。类型可以通过(可能带限定的)类型名称或类型字面量来指定,从而从先前声明的类型中组合出一个新类型。
>
> 类型 = 类型名称 | 类型字面量 | “(” 类型 “)”。
> 类型名称 = 标识符 | 限定标识符。
> 类型字面量 = 数组类型 | 结构体类型 | 指针类型 | 函数类型 | 接口类型 | 切片类型 | 映射类型 | 通道类型。
>
> 布尔、数值和字符串类型的命名实例是预声明的。可以使用类型字面量构造复合类型,如数组、结构体、指针、函数、接口、切片、映射和通道类型。
>
> 每个类型 T 都有一个底层类型:如果 T 是一个预声明类型或类型字面量,则相应的底层类型是 T 本身。否则,T 的底层类型是其类型声明中 T 引用的类型的底层类型。
>
> type T1 string
> type T2 T1
> type T3 []T1
> type T4 T3
>
> stringT1T2 的底层类型是 string[]T1T3T4 的底层类型是 []T1
>
> 类型转换
>
> 转换是形式为 T(x) 的表达式,其中 T 是一个类型,x 是一个可以转换为类型 T 的表达式。
>
> 非常量值 x 可以在以下情况下转换为类型 Tx 的类型和 T 具有相同的底层类型。

例如,

package main

import "fmt"

type StringSliceWrap []string

func main() {
	s := []string{"a", "b", "c"}
	ssw := StringSliceWrap(s)
	fmt.Println(ssw)
}

输出:

[a b c]
英文:

> The Go Programming Language Specification
>
> Types
>
> 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: If T is a predeclared type or a
> type literal, the corresponding underlying type is T itself.
> Otherwise, T's underlying type is the underlying type of the type to
> which T refers in its type declaration.
>
> type T1 string
> type T2 T1
> type T3 []T1
> type T4 T3
>
> The underlying type of string, T1, and T2 is string. The underlying
> type of []T1, T3, and T4 is []T1.
>
> Conversions
>
> Conversions are expressions of the form T(x) where T is a type and
> x is an expression that can be converted to type T.
>
> A non-constant value x can be converted to type T in the case:
> x's type and T 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]

huangapple
  • 本文由 发表于 2013年7月13日 10:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/17626331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定