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
+9k Golang : Generate random Chinese, Japanese, Korean and other runes
+14.4k Golang : Find commonalities in two slices or arrays example
+5.5k Linux : Disable and enable IPv4 forwarding
+10.7k Golang : Generate random elements without repetition or duplicate
+5.7k Golang : Use NLP to get sentences for each paragraph example
+27k Golang : Convert CSV data to JSON format and save to file
+8.9k Golang : Write multiple lines or divide string into multiple lines
+36.2k Golang : Display float in 2 decimal points and rounding up or down
+16.8k Golang : Covert map/slice/array to JSON or XML format
+16k Golang : Find out mime type from bytes in buffer
+12.1k Golang : Extract part of string with regular expression