[go] Go 언어의 경로 분리 및 결합
  1. 경로 분리
  2. 경로 결합

1. 경로 분리

Go 언어에서 경로를 분리하는 방법은 path/filepath 패키지의 Split 함수를 사용하는 것입니다.

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	dir, file := filepath.Split("/usr/local/bin/test.txt")
	fmt.Println("Directory:", dir)
	fmt.Println("File:", file)
}

위의 예제 코드에서 filepath.Split 함수를 사용하여 /usr/local/bin/test.txt라는 전체 경로를 디렉토리와 파일로 나누어 출력합니다.

2. 경로 결합

경로를 결합하는 방법은 path/filepath 패키지의 Join 함수를 사용하여 간단하게 할 수 있습니다.

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	newPath := filepath.Join("/usr", "local", "bin", "test.txt")
	fmt.Println("Joined path:", newPath)
}

위의 예제 코드에서 filepath.Join 함수를 사용하여 각 부분을 결합하여 /usr/local/bin/test.txt라는 경로를 생성합니다.

이와 같이 Go 언어에서는 path/filepath 패키지를 이용하여 경로를 분리하고 결합하는 기능을 제공합니다.


본문은 Go 공식 문서를 참고하여 작성되었습니다.