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
+10.1k Golang : Wait and sync.WaitGroup example
+12.3k Golang : Exit, terminating or aborting a program
+8.8k Golang : Capture text return from exec function example
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+19.7k Golang : Reset or rewind io.Reader or io.Writer
+13.2k Facebook PHP getUser() returns 0
+13.3k Golang : Strings comparison
+9k Golang : How to get username from email address
+7.8k Golang : Append and add item in slice
+5.1k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+7.8k Golang : Multiplexer with net/http and map
+4.5k MariaDB/MySQL : Form select statement or search query with Chinese characters