douhu5837 2016-06-15 19:26
浏览 24
已采纳

在多个Golang程序之间传递配置值

I am interested in passing configuration values between multiple golang programs.

I have experimented with environment variables but they cannot be read by a different program than the one that set them.

I have tested and I am certain that the environment variable is being set and can be read in the same process that set it. Also, if I call the second process through the first it will print it:

package main

import (
    "bufio"
    "fmt"
    "os"
)    

func main() {
    os.Setenv("AVARIABLE", "12345")

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    _, _ = reader.ReadString('
')
    fmt.Println("exiting")
}              

and

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println(os.Getenv("AVARIABLE"))
}

The environment variable context is too narrow for my purpose.

I am considering these options:

  1. Create a file at /etc/profile.d/<filename>.sh and run source /etc/profile.d/<filename>.sh in order to set the variable more globally.

  2. Create a file somewhere which anybody can read and avoid the environment variable issue.

Are there better ways? how should I proceed?

  • 写回答

1条回答 默认 最新

  • douxia1988 2016-06-15 20:25
    关注

    If you just want to share configuration that might change at runtime, then having a shared configuration file that is read in either periodically or whenever a change occurs. There are some mechanisms that you can use to notify you when a file changes, but those may be OS dependent.

    Another option is to use some form of inter-process communication but that may be a bit much if all you want to do is share some config variables.

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

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档