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.1k Linux : How to set root password in Linux Mint
+16.7k How to enable MariaDB/MySQL logs ?
+8.1k Golang : Populate or initialize struct with values example
+26.1k Golang : dial tcp: too many colons in address
+4.6k Golang : If else example and common mistake
+28.5k Golang : Login(Authenticate) with Facebook example
+10.4k Golang : How to pipe input data to executing child process?
+50.3k Golang : Check if item is in slice/array
+5.2k AWS S3 : Prevent Hotlinking policy
+28.3k Golang : JQuery AJAX post data to server and send data back to client example
+5.3k Java : Human readable password generator
+5.7k Golang : Combine slices of complex numbers and operation example