dprnr5559 2019-03-28 14:16
浏览 213

将类型转换为数组-GO语法

In the below syntax_1,

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

and syntax_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}
}

1) What does this syntax float64{7.0, 8.5, 9.1} & People{alex, john,jithesh} mean? this looks like a paradigm(a way of programming) more than a syntax

2) Can you provide reference to the meaning/purpose of [...] syntax? I see converting something to an [] type

  • 写回答

1条回答 默认 最新

  • douxiong5972 2019-03-28 14:44
    关注

    Squiggly brackets are used to define a composite literal. A value that contains more than one value inside it. In your first example an array of float values is created [...]float64{7.0, 8.5, 9.1} is a literal for an array of floats with 3 elements. In your second example, a few literals for predefined struct types are created and another array. Person{"alex", 21, "111-222-XXX"} is a literal for a struct of type Person. And [...]People{alex, john,jithesh} is an array of type People containing 3 elements.

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch