如何从字符串中删除结尾的“ ”

I tried with the following code but getting the same string in result:

package main

import (
    "fmt"
    "strings"
)

func main() {
    var s = "\b\x02\b\x02
"
    a := fmt.Sprintf("%q", s)
    fmt.Println("a:", a)
    b := strings.TrimRight(a, "
")
    fmt.Println("b:", b)
}

strings.TrimRight() works just fine. The "problem" in your case is that the string value stored in the a variable does not end with " ".

The reason for that is because you "quote" it using fmt.Sprintf(), and the string will end with "\\ ", and additionally even a double quotation mark will be added to it (that is, it ends with a backslash, the letter r, another backslash, the letter n and a double quote character).

If you don't quote your string, then:

var s = "\b\x02\b\x02
"
fmt.Printf("s: %q
", s)
b := strings.TrimRight(s, "
")
fmt.Printf("b: %q
", b)

Output (try it on the Go Playground):

s: "\b\x02\b\x02
"
b: "\b\x02\b\x02"