matlabmann 2014-05-08 23:13
浏览 182
已采纳

在包的init函数中初始化变量是否可以确保golang应用中的单个实例?

I am writing a Bloom Filter package. Within my package, I want to create an instance of X (non-exported) which I want to exist only as a single instance; no matter how many times my package is imported into an app.

A simple snippet of what I am doing is:

package superduperbloomfilter

var x X

func init() {
    x = X.New(....)
}

Is this correct ?

  • 写回答

1条回答 默认 最新

  • doukun8944 2014-05-08 23:20
    关注

    which I want to exist only as a single instance; no matter how many times my package is imported into an app

    The spec says an imported package is initialized only once:

    If a package has imports, the imported packages are initialized before initializing the package itself. If multiple packages import a package P, P will be initialized only once.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决