从golang代码重新加载或获取/ etc / environment文件

在将env变量写入/ etc / enviornment文件之后,我已经尝试了go代码中的source命令。

下面是示例代码。

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "os/exec"
    "strings"
)

func main() {
    address := "localhost:9090"
    file, err := os.OpenFile("/etc/environment", os.O_RDWR, 0644)
    defer file.Close()
    if err != nil {
        panic(err)
    }
    input, err := ioutil.ReadAll(file)
    if err != nil {
        log.Fatalln(err)
    }

    lines := strings.Split(string(input), "\n")

    for i, line := range lines {
        if strings.Contains(line, "HTTP_PROXY") {
            lines[i] = "HTTP_PROXY=" + address
        } else {
            if i == (len(lines) - 1) {
                lines[i] = "HTTP_PROXY=" + address
            }
        }
    }
    output := strings.Join(lines, "\n")
    err = ioutil.WriteFile("/etc/environment", []byte(output), 0644)
    if err != nil {
        log.Fatalln(err)
    }

    cmd := exec.Command("bash", "-c", "source /etc/environment")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err = cmd.Run()
    fmt.Println("cmd=================>", cmd, err)

    if err != nil {
        fmt.Println(err)
    }
}

它没有返回任何错误

But when i try to check my HTTP_PROXY in using env | grep -i proxy I dont see it getting reflected. I can only see the change is done when it restart the system or run the source command again from another termimal