我试图在我的程序中将原始字节字符串转换为 UUID,如下所示:
情况1:
package main
import (
"fmt"
"strconv"
"github.com/google/uuid"
)
func main() {
s := `"\220\254\0021\265\235O~\244\326\216\"\227c\245\002"`
s2, err := strconv.Unquote(s)
if err != nil {
panic(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
panic(err)
}
fmt.Println(u.String())
}
输出:
90ac0231-b59d-4f7e-a4d6-8e229763a502
案例2:
package main
import (
"fmt"
"strconv"
"github.com/google/uuid"
)
func main() {
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
s2, err := strconv.Unquote(s)
if err != nil {
panic(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
panic(err)
}
fmt.Println(u.String())
}
输出:
panic: invalid syntax
goroutine 1 [running]:
main.main()
/tmp/sandbox1756881518/prog.go:14 +0x149
Program exited.
上面的程序正在处理字符串"\220\254\0021\265\235O~\244\326\216\"\227c\245\002",但无法将字符串转换"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"为 uuid。如何将这些字符串转换为 UUID?
慕婉清6462132
相关分类