×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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 »





Comments and Discussions!

Load comments ↻





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