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
+12k Golang : Convert decimal number(integer) to IPv4 address
+10.7k Golang : Create matrix with Gonum Matrix package example
+19.8k Golang : Set or Add HTTP Request Headers
+16k Golang : Read a file line by line
+5.6k Javascript : How to loop over and parse JSON data?
+8.6k Golang : How to check variable or object type during runtime?
+18.4k Golang : Put UTF8 text on OpenCV video capture image frame
+18.7k Golang : Set, Get and List environment variables
+29.7k Golang : Saving(serializing) and reading file with GOB
+4.8k Javascript : Access JSON data example
+19.7k Golang : Close channel after ticker stopped example
+9.7k Javascript : Read/parse JSON data from HTTP response