在多个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?

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.