Getting specific files

Report a typo

Erick wants to create a Go program that takes as input a string fileExtension and then uses the filepath.Walk() function to walk the ./homework directory and print the file names that match the input fileExtension.

The program should only print the file names that match the input extension without the path, for example:

> .txt
biology_notes.txt

Tip: To get the file extension, you'll need to use the filepath.Ext() function, and to print only the file names (without the path), you'll need to use the filepath.Base() function.

Write a program
package main

import (
"fmt"
"log"
"os"
"path/filepath"
)

func main() {
var fileExtension string
fmt.Scanln(&fileExtension)

err := filepath.Walk("./homework", func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
// Write the code to check all the files with the input `fileExtension` in the `./homework` directory:
if filepath.?(?) == ? {
// Print the file name without the path below:
fmt.Println(?)
}
return nil
})
if err != nil {
log.Fatal(err)
}
}

___

Create a free account to access the full topic