×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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 »





Comments and Discussions!

Load comments ↻





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