×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to print environment detail

Here, we are going to learn how to print environment detail in Golang (Go Language)?
Submitted by Nidhi, on April 19, 2021 [Last updated : March 05, 2023]

Printing the environment detail in Golang

Problem Solution:

In this program, we will get environment detail using os.Environ() function and then access environment detail using range and print on the console screen.

Program/Source Code:

The source code to print the environment detail is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

Golang code to print the environment detail

// Golang program to print environment detail

package main

import "fmt"
import "os"
import "strings"

func main() {
	var envDetails []string = os.Environ()

	for envIndex, envVar := range envDetails {
		Value := strings.Split(envVar, "=")
		fmt.Printf("%d-> (%s: %s)\n", envIndex, Value[0], Value[1])
	}
}

Output:

0-> (SHELL: /bin/bash)
1-> (SESSION_MANAGER: local/ubuntu:@/tmp/.ICE-unix/1627,unix/ubuntu:/tmp/.ICE-unix/1627)
2-> (QT_ACCESSIBILITY: 1)
3-> (COLORTERM: truecolor)
4-> (XDG_CONFIG_DIRS: /etc/xdg/xdg-ubuntu:/etc/xdg)
5-> (XDG_MENU_PREFIX: gnome-)
6-> (GNOME_DESKTOP_SESSION_ID: this-is-deprecated)
7-> (GNOME_SHELL_SESSION_MODE: ubuntu)
8-> (SSH_AUTH_SOCK: /run/user/1000/keyring/ssh)
9-> (XMODIFIERS: @im)
10-> (DESKTOP_SESSION: ubuntu)
11-> (SSH_AGENT_PID: 1589)
12-> (GTK_MODULES: gail:atk-bridge)
13-> (PWD: /home/arvind/Downloads)
14-> (LOGNAME: arvind)
15-> (XDG_SESSION_DESKTOP: ubuntu)
16-> (XDG_SESSION_TYPE: x11)
17-> (GPG_AGENT_INFO: /run/user/1000/gnupg/S.gpg-agent:0:1)
18-> (XAUTHORITY: /run/user/1000/gdm/Xauthority)
19-> (GJS_DEBUG_TOPICS: JS ERROR;JS LOG)
20-> (WINDOWPATH: 2)
21-> (HOME: /home/arvind)
22-> (USERNAME: arvind)
23-> (IM_CONFIG_PHASE: 1)
24-> (LANG: en_US.UTF-8)
25-> (LS_COLORS: rs)
26-> (XDG_CURRENT_DESKTOP: ubuntu:GNOME)
27-> (VTE_VERSION: 6003)
28-> (GNOME_TERMINAL_SCREEN: /org/gnome/Terminal/screen/aafd945a_1a1c_4806_8a28_3e2b3a90c70c)
29-> (INVOCATION_ID: 027d0cc901a2488aafdd5b321e9ec89c)
30-> (MANAGERPID: 1416)
31-> (GJS_DEBUG_OUTPUT: stderr)
32-> (LESSCLOSE: /usr/bin/lesspipe %s %s)
33-> (XDG_SESSION_CLASS: user)
34-> (TERM: xterm-256color)
35-> (LESSOPEN: | /usr/bin/lesspipe %s)
36-> (USER: arvind)
37-> (GNOME_TERMINAL_SERVICE: :1.77)
38-> (DISPLAY: :0)
39-> (SHLVL: 1)
40-> (QT_IM_MODULE: ibus)
41-> (XDG_RUNTIME_DIR: /run/user/1000)
42-> (JOURNAL_STREAM: 8:48276)
43-> (XDG_DATA_DIRS: /usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop)
44-> (PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/local/go/bin:/usr/local/go/bin)
45-> (GDMSESSION: ubuntu)
46-> (DBUS_SESSION_BUS_ADDRESS: unix:path)
47-> (OLDPWD: /home/arvind)
48-> (_: /usr/local/go/bin/go)

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the "fmt" package to use the Printf() function and we also imported the "os" package to use the Environ() function.

In the main() function, we got environment detail using os.Environ() function. Then we accessed environment detail one by one using range and printed the result on the console screen.

Golang os Package Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.