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 Golang : Timeout example
+7.5k Golang : Example of custom handler for Gorilla's Path usage.
+8.3k Golang : How To Use Panic and Recover
+13.8k Android Studio : Password input and reveal password example
+11k Golang : Sieve of Eratosthenes algorithm
+5.1k Golang : Calculate a pip value and distance to target profit example
+17.5k Golang : Get future or past hours, minutes or seconds
+16k Golang : Get digits from integer before and after given position example
+8.2k Golang : Get all countries phone codes
+5.8k Get website traffic ranking with Similar Web or Alexa
+5.4k PHP : Hide PHP version information from curl
+5.5k How to check with curl if my website or the asset is gzipped ?