double2022
double2022
2014-07-16 16:16

具有多个字段的嵌入式结构的文字初始化

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术
type fun struct {}

type starcraft struct {
    *fun // embedding struct
    mu sync.Mutex
}

I know I can literal initial struct startcraft as:

f := &fun{}
s := starcraft{f, *new(sync.Mutex)}

I don't like it, since:

a. I don't want to initialize sync.Mutex by myself

b. in this case there is a wasted copy using *new(sync.Mutex).

Is there any better way?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐