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
+8.4k Golang : How to check if input string is a word?
+9.4k Mac OSX : Get a process/daemon status information
+10.6k Golang : Underscore string example
+10.1k Golang : How to tokenize source code with text/scanner package?
+6.3k CodeIgniter : form input set_value cause " to become & quot
+13.6k Golang : Activate web camera and broadcast out base64 encoded images
+12k Golang : Perform sanity checks on filename example
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+10.5k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+6.7k Golang : Experimental emojis or emoticons icons programming language
+22.1k Golang : Match strings by wildcard patterns with filepath.Match() function