WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Intermediate · question 39 of 100

Explain how you can interact with the file system using Go.?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

In Go, you can interact with the file system using the ‘os‘ and ‘io/ioutil‘ standard library packages. These packages provide functions to perform various file system operations such as creating, opening, reading, writing, and closing files, as well as manipulating directories and file attributes.

Here are some commonly used functions and how to use them in Go:

1. **Creating a new file**: You can create a new file using the ‘os.Create‘ function, which returns a file pointer.

file, err := os.Create("filename.txt")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

2. **Opening an existing file**: To open an existing file, use the ‘os.Open‘ function.

file, err := os.Open("filename.txt")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

3. **Reading a file**: You can read the content of a file using either the ‘os‘ and ‘io‘ packages or the ‘io/ioutil‘ package. The ‘io/ioutil‘ package provides a convenient helper function called ‘ioutil.ReadFile‘.

content, err := ioutil.ReadFile("filename.txt")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("File contents: %s", content)

4. **Writing to a file**: To write data to a file, you can use the ‘Write‘ or ‘WriteString‘ methods provided by the ‘os.File‘ type or the ‘ioutil.WriteFile‘ function.

content := []byte("Hello, World!")
err := ioutil.WriteFile("filename.txt", content, 0644)
if err != nil {
    log.Fatal(err)
}

5. **Creating and changing directories**: The ‘os.Mkdir‘ and ‘os.MkdirAll‘ functions can be used to create directories. The ‘os.Chdir‘ function can be used to change the current working directory.

err := os.Mkdir("new_directory", 0755)
if err != nil {
    log.Fatal(err)
}

err = os.Chdir("new_directory")
if err != nil {
    log.Fatal(err)
}

6. **Listing files in a directory**: To list all files in a directory, use the ‘ioutil.ReadDir‘ function.

files, err := ioutil.ReadDir(".")
if err != nil {
    log.Fatal(err)
}
for _, file := range files {
    fmt.Println(file.Name())
}

7. **Manipulating file attributes**: The ‘os‘ package provides several functions for manipulating file attributes, such as changing file permissions, ownership, modification times, etc. Some of these functions include ‘os.Chmod‘, ‘os.Chown‘, and ‘os.Chtimes‘.

err = os.Chmod("filename.txt", 0644)
if err != nil {
    log.Fatal(err)
}

For an example showing how to perform some of these operations, consider the following code:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
)

func main() {
    err := ioutil.WriteFile("example.txt", []byte("Hello, World!n"), 0644)
    if err != nil {
        log.Fatal(err)
    }

    content, err := ioutil.ReadFile("example.txt")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("File contents: %s", content)

    err = os.Remove("example.txt")
    if err != nil {
        log.Fatal(err)
    }
}

In this example, we create a new file ‘example.txt‘, write "Hello, World!" to it, read its contents, and finally delete the file.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics