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
+26.4k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+8.5k Golang : interface - when and where to use examples
+10.6k Golang : Split strings into command line arguments
+18.2k Golang : Archive directory with tar and gzip
+19.3k Golang : Convert PNG transparent background image to JPG or JPEG image
+16k Golang : Qt image viewer example
+5.8k Swift : substringWithRange() function example
+11.2k Golang : Arithmetic operation with numerical slices or arrays example
+8.6k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+19.7k Golang : Encrypt and decrypt data with TripleDES
+9.7k Golang : Sieve of Eratosthenes algorithm
+4.1k Golang : Constant and variable names in native language