Logo

Programming-Idioms

History of Idiom 151 > diff from v1 to v2

Edit summary for version 2 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 1

2016-10-24, 12:38:11

Version 2

2016-10-24, 12:41:33

Idiom #151 Remove string trailing path separator

Remove last character from string p, if this character is the file path separator of current platform.

Note that this also transforms unix root path "/" into empty string!

Idiom #151 Remove string trailing path separator

Remove last character from string p, if this character is the file path separator of current platform.

Note that this also transforms unix root path "/" into empty string!

Imports
import "fmt"
import "path/filepath"
import "strings"
Code
sep := fmt.Sprintf("%c", filepath.Separator)
p = strings.TrimSuffix(p, sep)
Comments bubble
filepath.Separator is a rune, it must be converted to string.
Doc URL
https://golang.org/pkg/strings/#TrimSuffix
Demo URL
https://play.golang.org/p/UevFigLqW_