Golang : File path independent of Operating System
There are times a program need to determine the operating system it is running on and construct a proper file path for file I/O.
Go has the http://golang.org/pkg/path/filepath/ package that will handle the cross plat form / operating system
Just use string(filepath.Separator)
through out your code will do.
Below is the an example code in Go:
package main
import (
"fmt"
"runtime"
"path/filepath"
)
func main() {
detect := "This OS is " + runtime.GOOS + " type and use "+ string(filepath.Separator) + " as file separator"
fmt.Println(detect)
}
The output will be
Unix/Linux
:
This OS is linux type and use / as file separator
Windows
:
This OS is windows type and use \ as file separator
See also : Golang : Detect (OS) Operating System
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.5k Javascript : Read/parse JSON data from HTTP response
+6.6k Golang : Skip or discard items of non-interest when iterating example
+9.3k Golang : Get all countries currencies code in JSON format
+22.3k Golang : How to read JPG(JPEG), GIF and PNG files ?
+19.7k Golang : Append content to a file
+16.3k Golang : How to implement two-factor authentication?
+3.5k Java : Get FX sentiment from website example
+4.7k Javascript : How to get width and height of a div?
+5.4k Clean up Visual Studio For Mac installation failed disk full problem
+4.8k Unix/Linux : secure copying between servers with SCP command examples
+8.7k Golang : Heap sort example
+19.7k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it