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
+5.4k Javascript : How to refresh page with JQuery ?
+13.2k Facebook PHP getUser() returns 0
+9.4k Javascript : Read/parse JSON data from HTTP response
+6.1k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+6.4k Golang : Embedded or data bundling example
+19.9k Swift : Convert (cast) Int to int32 or Uint32
+22.5k Golang : Round float to precision example
+45.8k Golang : Read tab delimited file with encoding/csv package
+10.6k PHP : Convert(cast) bigInt to string
+8.6k Golang : Accept any number of function arguments with three dots(...)
+8.6k Golang : Random integer with rand.Seed() within a given range
+8.1k Useful methods to access blocked websites