Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string replace backslash with slash

Tags:

go

I need to replace \ with / in a path string, but following code failed.

package main

import (
    "fmt"
    "strings"
)

func main() {
    string := "P:\Project\project-name/content/topic/"
    fmt.Println(strings.Replace(string, "\\", "/", -1))
}

Any helpful suggestions?

like image 439
kOssi Avatar asked Sep 26 '26 21:09

kOssi


1 Answers

Use the function filepath.ToSlash to replace the operating system path separator with '/' in a path.

On Windows, the function returns strings.Replace(path, string(filepath.Separator), "/", -1). On other operating systems, the function returns the path argument as is.