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
+21.3k Golang : How to get time zone and load different time zone?
+11.9k Golang : convert(cast) float to string
+30.1k Golang : Get time.Duration in year, month, week or day
+12.1k Golang : Clean formatting/indenting or pretty print JSON result
+8k Golang Hello World Example
+4.9k Golang : A program that contain another program and executes it during run-time
+41.3k Golang : How to count duplicate items in slice/array?
+4.9k Which content-type(MIME type) to use for JSON data
+5.8k Fix yum-complete-transaction error
+16.1k Golang : How to reverse elements order in map ?
+10.7k Golang : Get local time and equivalent time in different time zone
+21.7k Golang : GORM create record or insert new record into database example