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
+3.8k Golang : Experimental Jawi programming language
+8.4k Golang : Accessing content anonymously with Tor
+5.9k Golang : Takes a plural word and makes it singular
+6.8k Android Studio : Rating bar example
+31.2k Golang : How to check if a date is within certain range?
+3.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+5.6k Golang : Totalize or add-up an array or slice example
+4.6k PHP : Convert CSV to JSON with YQL example
+8.6k Golang : Validate IPv6 example
+12.6k Golang : How to determine if a year is leap year?
+4.4k Unix/Linux/MacOSx : How to remove an environment variable ?