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
+10.1k Golang : How to profile or log time spend on execution?
+9.1k Golang : Write multiple lines or divide string into multiple lines
+5.9k Golang : Shuffle array of list
+5.1k Golang : Customize scanner.Scanner to treat dash as part of identifier
+5.5k Golang : Detect words using using consecutive letters in a given string
+6.5k Golang : Convert an executable file into []byte example
+15.1k Golang : Get HTTP protocol version example
+11k Google Maps URL parameters configuration
+8.9k Golang : automatically figure out array length(size) with three dots
+14.3k Golang : On enumeration
+13.7k Golang : Gin framework accept query string by post request example
+7.7k Golang : Load DSA public key from file example