Created
April 25, 2018 01:00
-
-
Save DanHam/449fc0bce0e5f33b6da3691baba11389 to your computer and use it in GitHub Desktop.
Demonstrate filepath.[Dir|Base|ToSlash] behaves differently depending on platform and path format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"runtime" | |
) | |
const ( | |
Separator = os.PathSeparator | |
) | |
func main() { | |
fmt.Printf("OS is %s\n", runtime.GOOS) | |
fmt.Printf("Path separator is: %s\n\n", string(Separator)) | |
forward_path := `C:/Windows/Temp/forward.txt` | |
fmt.Printf("Forward path is %s\n", forward_path) | |
fmt.Printf("filepath.Dir(`%s`) gives: %s\n", forward_path, filepath.Dir(forward_path)) | |
fmt.Printf("filepath.Base(`%s`) gives: %s\n\n", forward_path, filepath.Base(forward_path)) | |
forward_path_space := `C:/Windows/Temp/forward\ with\ space.txt` | |
fmt.Printf("Forward path with spaces is %s\n", forward_path_space) | |
fmt.Printf("filepath.Dir(`%s`) gives: %s\n", forward_path_space, filepath.Dir(forward_path_space)) | |
fmt.Printf("filepath.Base(`%s`) gives: %s\n\n", forward_path_space, filepath.Base(forward_path_space)) | |
backward_path := `C:\Windows\Temp\backward.txt` | |
fmt.Printf("Backward path is %s\n", backward_path) | |
fmt.Printf("filepath.Dir(`%s`) gives: %s\n", backward_path, filepath.Dir(backward_path)) | |
fmt.Printf("filepath.Base(`%s`) gives: %s\n\n", backward_path, filepath.Base(backward_path)) | |
converted_path := filepath.ToSlash(backward_path) | |
fmt.Printf("Converting the backward path with filepath.ToSlash(`%s`) gives: %s\n", backward_path, filepath.ToSlash(backward_path)) | |
fmt.Printf("For the converted backward path filepath.Dir(`%s`) gives %s\n", converted_path, filepath.Dir(converted_path)) | |
fmt.Printf("For the converted backward path filepath.Base(`%s`) gives %s\n\n", converted_path, filepath.Base(converted_path)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment