duananyantan04633 2016-08-28 09:43
浏览 38
已采纳

为什么* S Slice可以由文字S开头?

I saw this syntax in page187 of the go programming language.

var tracks = []*Track{
    {"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
    {"Go", "Moby", "Moby", 1992, length("3m37s")},
    {"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
    {"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}

Is it just a syntax sugar of

var tracks = []*Track{
    &Track{"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
    &Track{"Go", "Moby", "Moby", 1992, length("3m37s")},
    &Track{"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
    &Track{"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}

I haven't googled the specification of it, please give me the link if there is.

  • 写回答

1条回答 默认 最新

  • duanjianxiu9400 2016-08-28 09:57
    关注

    From the ref spec

    Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T.

    [...]*Point{{1.5, -3.5}, {0, 0}}    // same as [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真