To get the current working directory of a process using syscalls in Go, you can use the syscall.Getwd()
function. Here's an example:
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
pid := os.Getpid()
cwd, err := syscall.Getwd()
if err != nil {
fmt.Println("Error getting current working directory:", err)
return
}
fmt.Printf("Current working directory of process %d: %s\n", pid, cwd)
}
In this example, we first retrieve the process ID (PID) using os.Getpid()
. Then, we use syscall.Getwd()
to get the current working directory. If there is an error, we handle it accordingly. Finally, we print the current working directory along with the process ID.