在Golang中如何创建一个以全局变量为值的地图(map)?

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

How to create map in Golang with global variable as value?

问题

我有一个方法来检查是否存在所需的环境变量,如果存在,则设置它,否则填充错误消息。我正在按照以下方式进行操作:

package main

import (
	"errors"
	"fmt"
	"os"
	"strings"
)

var (
	AxGitDownloadUrl string
)

const ENV_ERR_MSG_START = "set the following environment variables ->"
const AX_GIT_DOWNLOAD_URL = "AX_GIT_DOWNLOAD_URL"

func main() {
	if err := verifyMandatoryEnvVarsAndSet(); err != nil {
		fmt.Printf(err.Error())
	}
}

func verifyMandatoryEnvVarsAndSet() error {
	errMsg := ENV_ERR_MSG_START
	m := map[string]string{AX_GIT_DOWNLOAD_URL: AxGitDownloadUrl}

	for k, v := range m {
		errMsg = setConfig(k, v, errMsg)
	}
	if len(errMsg) > len(ENV_ERR_MSG_START) {
		errMsg = strings.TrimSuffix(errMsg, ",")
		return errors.New(errMsg)
	}
	return nil
}

func setConfig(envVarName, globalEnvVar, errMsg string) string {
	fmt.Printf("envVarName -> %s, globalEnvVar -> %s ", envVarName, globalEnvVar)
	res, ok := os.LookupEnv(envVarName)
	if ok {
		globalEnvVar = res
	} else {
		errMsg = errMsg + envVarName + ","
	}
	return errMsg
}

我遇到的问题是,在映射的值部分,它没有设置全局变量,而是显示为空。

那么,如何使用setConfig方法设置全局变量的值呢?

Go Playground链接:https://go.dev/play/p/DC5oPmsjEIQ

英文:

I have a method to check if a required environment variable exists or not, if yes then set it, else populate an error message. I am doing like the following:

package main

import (
	"errors"
	"fmt"
	"os"
	"strings"
)

var (
	AxGitDownloadUrl string
)

const ENV_ERR_MSG_START = "set the following environment variables ->"
const AX_GIT_DOWNLOAD_URL = "AX_GIT_DOWNLOAD_URL"

func main() {
	if err := verifyMandatoryEnvVarsAndSet(); err != nil {
		fmt.Printf(err.Error())
	}
}
func verifyMandatoryEnvVarsAndSet() error {
	errMsg := ENV_ERR_MSG_START
	m := map[string]string{AX_GIT_DOWNLOAD_URL: AxGitDownloadUrl}

	for k, v := range m {
		errMsg = setConfig(k, v, errMsg)
	}
	if len(errMsg) > len(ENV_ERR_MSG_START) {
		errMsg = strings.TrimSuffix(errMsg, ",")
		return errors.New(errMsg)
	}
	return nil
}

func setConfig(envVarName, globalEnvVar, errMsg string) string {
	fmt.Printf("envVarName -> %s,globalEnvVar->%s ", envVarName, globalEnvVar)
	res, ok := os.LookupEnv(envVarName)
	if ok {
		globalEnvVar = res
	} else {
		errMsg = errMsg + envVarName + ","
	}
	return errMsg
}

The problem I am facing is, in the value part of the map, it is not setting up the global variable, rather I am seeing a blank there.

So how can I set up the global variable value using the setConfig method?

Go playground link: https://go.dev/play/p/DC5oPmsjEIQ

答案1

得分: 1

你在尝试的操作中,如果不使用指针是无法实现的。变量globalEnvVar与全局变量没有连接。它是一个位于内存中不同地址的不同对象,它们的内容可能相同,但更新一个对象不会更新另一个对象。

将map改为map[string]*string,并使用指向你想要更新的变量的指针。在进行更新时,使用指针间接引用,例如*globalEnvVar = res

英文:

You can't do what you're trying to do without using pointers. The variable globalEnvVar has no connection to the global var. It's a different object located at a different address in the memory, their contents may be the same but updating one object will not update the other.

Change the map to map[string]*string and use pointers-to-variables that you want to update. And when doing the updating use pointer indirection, e.g. *globalEnvVar = res.

huangapple
  • 本文由 发表于 2022年10月13日 15:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/74052261.html
匿名

发表评论

匿名网友

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

确定