首页 > 解决方案 > 将类型转换为数组 - GO 语法

问题描述

在下面的语法_1中,

 array := [...]float64{7.0, 8.5, 9.1}

和语法_2,

type People interface {
    SayHello()
    ToString()
}

type Student struct {
    Person
    university string
    course string
}

type Developer struct {
    Person
    company string
    platform string
}


func main(){

    alex := Student{Person{"alex", 21, "111-222-XXX"}, "MIT","BS CS"}
    john := Developer{Person{"John", 35, "111-222-XXX"}, "Accel North America", "Golang"}
    jithesh := Developer{Person{"Jithesh", 33, "111-222-XXX"}, "Accel North America", "Hadoop"}
    //An array with People types    
    peopleArr := [...]People{alex, john,jithesh}
}

float64{7.0, 8.5, 9.1}1 )这个语法People{alex, john,jithesh}是什么意思?这看起来更像是一种范式(一种编程方式)而不是一种语法

2)您能否提供[...]语法含义/目的的参考?我看到将某些东西转换为[]类型

标签: go

解决方案


波浪括号用于定义复合文字。一个包含多个值的值。在您的第一个示例中,创建的浮点值 [...]float64{7.0, 8.5, 9.1}数组是具有 3 个元素的浮点数组的文字。在您的第二个示例中,创建了一些预定义结构类型的文字和另一个数组。Person{"alex", 21, "111-222-XXX"}是 Person 类型结构的文字。并且[...]People{alex, john,jithesh}是包含 3 个元素的 People 类型的数组。


推荐阅读