find the extension of a json file in go

To find the extension of a JSON file in Go, you can use the filepath and strings packages. Here's an example code snippet to achieve this:

main.go
package main

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

func main() {
    filePath := "/path/to/file.json"
    extension := filepath.Ext(filePath)
    extension = strings.TrimPrefix(extension, ".")
    fmt.Println(extension) // Output: json
}
257 chars
15 lines

In this code, we first define the filepath of the JSON file in the filePath variable. Then, we use the filepath.Ext() function to get the extension of the file (including the '.' prefix). Finally, we remove the '.' prefix using the strings.TrimPrefix() function and print the extension.

gistlibby LogSnag