To find the name of a JSON file in Go, you can use the ioutil
package to read the contents of a directory and then loop through each file to check if it has a .json
extension. Once you find the file with the JSON data, you can return its name as a string.
Here's an example code snippet:
main.go397 chars23 lines
In this code, we first set the dirPath
variable to the path of the directory containing our JSON files. We then use ioutil.ReadDir
to get a slice of os.FileInfo
describing each file in the directory.
We then loop through each file using a for ... range
loop and check if the file has a .json
extension using filepath.Ext
. If we find a file with a .json
extension, we print its name using fmt.Println
and break out of the loop using break
.
gistlibby LogSnag