Golang os.Getwd() Function with Examples

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

os.Getwd()

In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The Getwd() function is an inbuilt function of the os package, it is used to get the rooted pathname corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd() function may return any one of them.

It accepts nothing and returns a string containing the rooted pathname corresponding to the current directory and an error if any.

Syntax:

func Getwd() (dir string, err error)

Parameter(s):

  • None

Return Value:

The return type of the os.Getwd() function is (dir string, err error), it returns a string containing the rooted pathname corresponding to the current directory and an error if any.

Example:

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

package main

import (
	"fmt"
	"os"
)

func main() {
	str, err := os.Getwd()

	fmt.Printf("str: %T, %v\n", str, str)
	fmt.Printf("err: %T, %v\n", err, err)
}

Output:

str: string, /
err: <nil>, <nil>

Golang os Package »





Comments and Discussions!

Load comments ↻






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