Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

cd command in Go or change current working directory in Go (Golang)

Posted on April 17, 2020April 18, 2020 by admin

Overview

os.Chdir() is used to change the current working directory to the named directory in golang. It is similar to the cd command.

Below is the signature of the function

func Chdir(dir string) error

Code

package main

import (
    "fmt"
    "os"
)

func main() {
    os.Chdir("/Users")
    newDir, err := os.Getwd()
    if err != nil {
    }
    fmt.Printf("Current Working Direcotry: %s\n", newDir)
}

Output:

Current Working Direcoty: /Users
  • cd command
  • go
  • golang
  • Follow @golangbyexample

    Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme