10 examples of 'golang environment variables' in Go

Every line of 'golang environment variables' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
118func (b *ContainerBuilder) Env(vars ...*corev1api.EnvVar) *ContainerBuilder {
119 for _, v := range vars {
120 b.object.Env = append(b.object.Env, *v)
121 }
122 return b
123}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

18func Environ(c *yaml.Config, envs map[string]string) error {
19 var images []*yaml.Container
20 images = append(images, c.Pipeline...)
21 images = append(images, c.Services...)
22
23 for _, p := range images {
24 if p.Environment == nil {
25 p.Environment = map[string]string{}
26 }
27 for k, v := range envs {
28 if v == "" {
29 continue
30 }
31 p.Environment[k] = v
32 }
33 if httpProxy != "" {
34 p.Environment["HTTP_PROXY"] = httpProxy
35 p.Environment["http_proxy"] = strings.ToUpper(httpProxy)
36 }
37 if httpsProxy != "" {
38 p.Environment["HTTPS_PROXY"] = httpsProxy
39 p.Environment["https_proxy"] = strings.ToUpper(httpsProxy)
40 }
41 if noProxy != "" {
42 p.Environment["NO_PROXY"] = noProxy
43 p.Environment["no_proxy"] = strings.ToUpper(noProxy)
44 }
45 }
46 return nil
47}
71func addEnvironmentVariable(cmd *cobra.Command, args []string) {
72 var err error
73 if len(args) != 5 {
74 sdk.Exit("Wrong usage: %s\n", cmd.Short)
75 }
76 projectKey := args[0]
77 envName := args[1]
78 varName := args[2]
79 varValue := args[3]
80 varType := args[4]
81
82 if *force {
83 variables, errSh := sdk.ShowEnvironmentVariable(projectKey, envName)
84 if errSh != nil {
85 sdk.Exit("Error: cannot show existing variables for environment %s (%s)\n", envName, err)
86 }
87
88 varExist := false
89 for _, v := range variables {
90 if v.Name == varName {
91 varExist = true
92 break
93 }
94 }
95 if !varExist {
96 err = sdk.AddEnvironmentVariable(projectKey, envName, varName, varValue, varType)
97 } else {
98 err = sdk.UpdateEnvironmentVariable(projectKey, envName, varName, varName, varValue, varType)
99 }
100
101 } else {
102 err = sdk.AddEnvironmentVariable(projectKey, envName, varName, varValue, varType)
103 }
104
105 if err != nil {
106 sdk.Exit("Error: cannot add variable %s in environment %s (%s)\n", varName, envName, err)
107 }
108 fmt.Printf("OK\n")
109}
206func (l *local) SetEnv(key, val string) error {
207 if l.Inspect.Config == nil {
208 return fmt.Errorf("failed to set env var, image '%s' does not exist", l.RepoName)
209 }
210 l.Inspect.Config.Env = append(l.Inspect.Config.Env, fmt.Sprintf("%s=%s", key, val))
211 return nil
212}
217func env(name string, val interface{}) string {
218 return fmt.Sprintf("%s=%v", name, val)
219}
76func SetEnvironment(name string, value string) error {
77 err := wrappers.SetEnvironmentVariable(
78 syscall.StringToUTF16Ptr(name),
79 syscall.StringToUTF16Ptr(value))
80 if err != nil {
81 return NewWindowsError("SetEnvironmentVariable", err)
82 }
83 return nil
84}
160func (ts *Script) Setenv(key, value string) {
161 ts.env = append(ts.env, key+"="+value)
162 ts.envMap[envvarname(key)] = value
163}
1049func EnvironSetenv(envp unsafe.Pointer, variable *C.gchar, value *C.gchar, overwrite C.gboolean) unsafe.Pointer {
1050 return unsafe.Pointer(C._g_environ_setenv(unsafe.Pointer(envp), unsafe.Pointer(variable), unsafe.Pointer(value), overwrite))
1051}
33func SetEnv(vars Vars) {
34 _envLock.Lock()
35 _env = vars
36 _envLock.Unlock()
37}
67func Setenv(ck *C.TString, cv *C.TString) {
68 os.Setenv(C.GoStringN(ck.data, ck.size), C.GoStringN(ck.data, ck.size))
69}

Related snippets