程序必须访问包中的全局变量。

huangapple go评论92阅读模式
英文:

Programs have to access to global variables in package

问题

我有一个与打开的文件(*os.File)和与之关联的日志记录器相关的全局变量的包。
另一方面,我将构建几个命令来使用该包,我不想每次运行命令时都打开文件并将其设置为日志记录器。

因此,第一个要运行的程序将设置全局变量,这里是我的问题:

  • 后续使用该包的程序能够无问题地访问这些全局变量吗?可以创建一个带有标志的命令,在被其他程序使用之前初始化这些值,并在完成后取消设置包中的全局变量。

  • 如果这不可能,避免此类IO限制的最佳选项是什么?使用Unix套接字中的服务器。

英文:

I've a package with global variables related to a file open
(*os.File), and its logger associated.
By another side, I'll build several commands that are going to use
that package and I want not open the file to set it as logger every
time I run a command.

So, the first program to run will set the global variables, and here
is my question:

  • Do the next programs to use the package can access to those global
    variables without problem? It could be created a command with a flag
    to initialize those values before of be used by another programs, and
    another flag to finish it (unset the global variables in the package).

  • If that is not possible, which is the best option to avoid such IO-bound? To use a server in Unix sockets?

答案1

得分: 0

假设你所说的“program”实际上是指“进程”,那么答案是否定的。

如果你想在进程之间共享一个(可能是定制化的)日志功能,我会考虑使用类似守护进程的方式(据我所知,Go目前还不支持编写真正的守护进程),以及任何你认为方便的进程间通信方式。

英文:

Assuming by 'program' you actually mean 'process', the answer is no.

If you want to share a (customized perhaps) logging functionality between processes then I would consider a daemon-like (Go doesn't yet AFAIK support writing true daemons) process/server and any kind of IPC you see handy.

huangapple
  • 本文由 发表于 2012年5月29日 03:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/10789514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定