go - 将类型转换为数组 - 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)您能否提供[...]
语法含义/目的的参考?我看到将某些东西转换为[]
类型
解决方案
波浪括号用于定义复合文字。一个包含多个值的值。在您的第一个示例中,创建的浮点值
[...]float64{7.0, 8.5, 9.1}
数组是具有 3 个元素的浮点数组的文字。在您的第二个示例中,创建了一些预定义结构类型的文字和另一个数组。Person{"alex", 21, "111-222-XXX"}
是 Person 类型结构的文字。并且[...]People{alex, john,jithesh}
是包含 3 个元素的 People 类型的数组。
推荐阅读
- python - 如何根据每个元素中的某些信息对列表的元素进行分组?
- docker - 无法从 docker 运行 tensorflow 服务
- docker - 两个 docker 网络之间的 Http 请求
- python - 从两个单独的数组中的文件读取数据
- c# - 如何从任务列表中启动异步任务?或如何计算任务?
- c++ - 您如何使用 FFF 和 Google Test 在 C/C++ 中模拟和测试相同的函数?
- angular - Angular 9 测试错误 NullInjectorError: No provider for Window
- powerapps - 使用 ForAll 和 Patch 在 PowerApp 中更新或插入记录
- microsoft-teams - 设置自定义标签“新”徽章
- c++ - C++ 重载 * 以乘以不同类型的分数