How to change the working directory using syscall in Golang?

Here, we have to change the working directory using syscall in Golang.
Submitted by IncludeHelp, on November 12, 2021 [Last updated : March 05, 2023]

Changing the working directory using syscall in Golang

In the Go language, to change the working directory using syscall – we use the Chdir() function of the syscall package. The Chdir() function is used to change the working directory.

Syntax

func Chdir(path string) (err error)

Consider the below example demonstrating how to change the working directory using syscall in Golang?

Golang code to change the working directory using syscall

package main

import (
	"fmt"
	"syscall"
)

func main() {
	// Getting the current working directory
	CurrentWD, _ := syscall.Getwd()
	fmt.Println("CurrentWD:", CurrentWD)

	// Changing the working directory
	syscall.Chdir("/home/IncludeHelp/GoLang")

	// Again,
	// getting the current working directory
	CurrentWD, _ = syscall.Getwd()
	fmt.Println("CurrentWD:", CurrentWD)
}

Output

CurrentWD: /home
CurrentWD: /home/IncludeHelp/GoLang

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.