Golang os.Environ() Function with Examples

Golang | os.Environ() Function: Here, we are going to learn about the Environ() function of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on November 16, 2021

os.Environ()

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The Environ() function is an inbuilt function of the os package, it is used to get a copy of strings representing the environment, in the form "key=value".

It accepts nothing and returns a slice of the string containing the environment in the form of "key=value".

Syntax:

func Environ() []string

Parameter(s):

  • None

Return Value:

The return type of the os.Environ() function is []string, it returns a slice of string containing the environment in the form of "key=value".

Example:

// Golang program to demonstrate the
// example of Environ() function

package main

import (
	"fmt"
	"os"
)

func main() {
	// Getting the environment variables
	env := os.Environ()

	// Printing the return type & value
	fmt.Printf("Type : %T\n", env)
	fmt.Printf("Value: %v\n", env)
}

Output:

Type : []string
Value: [PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=6c78da983b38 HOME=/root]

Golang os Package »





Comments and Discussions!

Load comments ↻






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