If you're using Windows 11, simply right-click on it. Then, select “Copy as path” in the contextual menu. Alternatively, in Windows 10, you can also select the item (file, folder, library) and click or tap on the “Copy as path” button from File Explorer's Home tab in Windows 10.
From the "Text" group, click [Quick Parts] > Select "Field..." Under "Field names," select "FileName." In the "Field properties" section, select a format. In the "Field options" section, check "Add path to filename." The file name will now appear in the header or footer.
File paths are of two types: Absolute File Paths. Relative File Paths.
For creating and manipulating OS-specific paths directly use os.PathSeparator
and the path/filepath
package.
An alternative method is to always use '/'
and the path
package throughout your program. The path
package uses '/'
as path separator irrespective of the OS. Before opening or creating a file, convert the /-separated path into an OS-specific path string by calling filepath.FromSlash(path string)
. Paths returned by the OS can be converted to /-separated paths by calling filepath.ToSlash(path string)
.
Use path/filepath
instead of path
. path
is intended for forward slash-separated paths only (such as those used in URLs), while path/filepath
manipulates paths across different operating systems.
Based on the answer of @EvanShaw and this blog the following code was created:
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
p := filepath.FromSlash("path/to/file")
fmt.Println("Path: " + p)
}
returns:
Path: path\to\file
on Windows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With