Skip to content

Commit

Permalink
osext: use TrimSuffix.
Browse files Browse the repository at this point in the history
  • Loading branch information
kardianos committed Mar 17, 2015
1 parent 7ed26b5 commit 2a9b5b4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion osext_procfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func executable() (string, error) {
return execpath, err
}
if strings.HasSuffix(execpath, deletedSuffix) {
execpath = execpath[:len(execpath)-len(deletedSuffix)]
execpath = strings.TrimSuffix(execpath, deletedSuffix)
}
return execpath, nil
case "netbsd":
Expand Down

2 comments on commit 2a9b5b4

@dmitshur
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you misunderstood what I meant. The entire if statement can be replaced:

-if strings.HasSuffix(execpath, deletedSuffix) {
-   execpath = execpath[:len(execpath)-len(deletedSuffix)]
-}
+execpath = strings.TrimSuffix(execpath, deletedSuffix)

If you look at the source of strings.TrimSuffix, it already performs the HasSuffix if check:

// TrimSuffix returns s without the provided trailing suffix string.
// If s doesn't end with suffix, s is returned unchanged.
func TrimSuffix(s, suffix string) string {
    if HasSuffix(s, suffix) {
        return s[:len(s)-len(suffix)]
    }
    return s
}

@kardianos
Copy link
Owner Author

@kardianos kardianos commented on 2a9b5b4 Mar 17, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.