dongqiao0953
dongqiao0953
2015-07-08 22:57

此语法是指向切片的指针还是切片的指针?

已采纳

After looking at this blog post, I am still unclear about whether the following syntax means a slice of pointers or a pointer to a slice.

foo  []*int

Which one is it, and what is the general rule of thumb for this case?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douzhu6149 douzhu6149 6年前

    This is a slice of pointers, just read from left to right: ([]) a slice of (*) pointers to (int) integer. On the other hand, *[]int would be (*) a pointer to ([]) a slice of (int) integers.

    点赞 评论 复制链接分享
  • dongnanbi4942 dongnanbi4942 6年前

    That is a slice of int pointers. [] brackets come before the type so *int is the type in your example while *[]int is a pointer to an int array or *[]*int for a pointer to an array of int pointers.

    Just as a matter of style I would generally avoid using *[]int syntax in favor or something like foo := &[]int{} which results in foo being a pointer to the start of a new int array.

    点赞 评论 复制链接分享

相关推荐