How to get the environment variables using syscall in Golang?

Here, we will learn to get the environment variables using syscall in Golang.
Submitted by IncludeHelp, on November 12, 2021 [Last updated : March 05, 2023]

Getting the environment variables using syscall in Golang

In the Go programming language, to get environment variables using syscall – we use the Environ() function of the the syscall package. The Environ() function returns an array of strings populated with your environment variables.

Syntax

func Environ() []string

Consider the below example demonstrating how to get environment variables using syscall in Golang?

Golang code to get the environment variables using syscall

package main

import (
	"fmt"
	"syscall"
)

func main() {
	// Using the Environ() function
	// Getting the environment variables
	env := syscall.Environ()

	// Printing
	fmt.Println("Environment variables:")
	for i := range env {
		fmt.Println(env[i])
	}
}

Output

Environment variables:
LANGUAGE=en_US:en
HOSTNAME=Check
HOME=/
GOROOT=/usr/local/go
TERM=xterm
PATH=/opt/swift/swift-5.0-RELEASE-ubuntu14.04/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DISPLAY=:1
LANG=en_US.UTF-8
DEBIAN_FRONTEND=noninteractive
GOCACHE=/home/.cache
LC_ALL=en_US.UTF-8
PWD=/home

Golang syscall Package Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.