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
+4.9k Which content-type(MIME type) to use for JSON data
+18.8k Golang : Get download file size
+7.7k Golang : Shuffle strings array
+5.4k Golang : Convert lines of string into list for delete and insert operation
+18.2k Golang : Defer function inside init()
+23.6k Golang : Check if element exist in map
+10.4k Golang : Convert file content to Hex
+4.8k Mac OSX : Get disk partitions' size, type and name
+13.6k Golang : Read from buffered reader until specific number of bytes
+5k HTTP common errors and their meaning explained
+21.8k Golang : GORM create record or insert new record into database example
+8.6k Golang : How to check if input string is a word?