Go
golang

[Go] ファイルの拡張子を変更する

More than 1 year has passed since last update.

path/filepath を使って安全に。

package main

import (
"fmt"
"path/filepath"
)

func replaceExt(filePath, from, to string) string {
ext := filepath.Ext(filePath)
if len(from) > 0 && ext != from {
return filePath
}
return filePath[:len(filePath)-len(ext)] + to
}

/* Example */
func main() {
filePath := "inputDir/input.csv"

fmt.Println(filePath)
// => inputDir/input.csv

filePath = replaceExt(filePath, ".csv", ".json")
fmt.Println(filePath)
// => inputDir/input.json

filePath = replaceExt(filePath, ".json", "")
fmt.Println(filePath)
// => inputDir/input

fmt.Println(replaceExt("inputDir/input", ".csv", ".json"))
// => inputDir/input

fmt.Println(replaceExt("inputDir/input.rcsv.csv", ".csv", ".json"))
// => inputDir/input.rcsv.json
}