find the extension of a csv file in go

To find the extension of a CSV file in Go, you can use the path/filepath package. Here's an example code that demonstrates how to do this:

main.go
package main

import (
    "fmt"
    "path/filepath"
    "strings"
)

func main() {
    filename := "data.csv"
    ext := filepath.Ext(filename)
    if strings.ToLower(ext) == ".csv" {
        fmt.Println("The file is a CSV file")
    } else {
        fmt.Println("The file is not a CSV file")
    }
}
302 chars
18 lines

In this example, we first use the filepath package to get the extension of the file by calling filepath.Ext(filename). This function returns the extension of the file, including the dot (.).

We then convert the extension to lowercase using the strings.ToLower() function, and compare it with the lowercase .csv string. If they match, we print a message indicating that the file is a CSV file. Otherwise, we print a message indicating that the file is not a CSV file.

gistlibby LogSnag