[go] 경로 구문 분석

이번에는 Go 언어에서 파일 경로를 분석하는 방법에 대해 알아보겠습니다.

path/filepath 패키지

Go 언어에서 파일 경로를 다루는 path/filepath 패키지는 파일 경로를 조작하고 조작하기 쉽게 해주는 함수를 제공합니다.

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	path := "/home/user/example.txt"

	dir := filepath.Dir(path)
	fmt.Println("폴더 경로:", dir)

	file := filepath.Base(path)
	fmt.Println("파일 이름:", file)

	ext := filepath.Ext(path)
	fmt.Println("확장자:", ext)
}

위 예제는 path/filepath 패키지를 사용하여 파일 경로를 분석하는 간단한 예제입니다. filepath.Dir() 함수는 파일의 디렉토리 경로를 가져오고, filepath.Base() 함수는 파일의 이름을 가져오며, filepath.Ext() 함수는 파일의 확장자를 가져옵니다.

이렇게 하면 파일 경로를 손쉽게 다룰 수 있습니다.

더 자세한 내용은 Go 공식 문서를 참고하시기 바랍니다.