英文:
Go - Convert raw byte string into uuid
问题
我正在尝试将原始字节字符串转换为UUID。以下是我的程序:
案例1:
package main
import (
"fmt"
"strconv"
"github.com/google/uuid"
)
func main() {
s := `"04package 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())
}
2155O~4667c5package 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())
}
2"`
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 := `"53\'1package 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())
}
31@265o52package 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())
}
213"`
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
程序退出。
以上程序可以使用字符串"\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?
英文:
I am trying to convert raw byte strings into UUIDs in my program as follows:
Case1:
package main
import (
"fmt"
"strconv"
"github.com/google/uuid"
)
func main() {
s := `"04package 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())
}
2155O~466\"7c5package 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())
}
2"`
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())
}
output:
90ac0231-b59d-4f7e-a4d6-8e229763a502
Case2:
package main
import (
"fmt"
"strconv"
"github.com/google/uuid"
)
func main() {
s := `"53\'1package 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())
}
31@265o52package 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())
}
213"`
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())
}
output:
panic: invalid syntax
goroutine 1 [running]:
main.main()
/tmp/sandbox1756881518/prog.go:14 +0x149
Program exited.
The above program is working with string "\220\254\0021\265\235O~\244\326\216\"\227c\245\002"
but fails at converting string "\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"
into uuid. How can I convert these strings into UUIDs?
答案1
得分: 3
如果因为\'
而失败。当使用反引号时,所有的反斜杠都是字面上的反斜杠,而不是转义序列,所以你传递的是原始的反斜杠\
,后面跟着单引号'
给strconv.Unquote
。这导致了无效的语法。这里有两种解决方法:
第一种
只需将这行代码替换为:
s := `"53'1s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
213"`
而不是:
s := `"53\'1s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
213"`
所以这里是'
而不是\'
。但是如果你需要以编程方式转换字符串,请使用第二种方法。
第二种
导入"strings"
:
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
并将\'
替换为'
:
s = strings.ReplaceAll(s, `\'`, `'`)
所以完整的代码现在看起来像这样:
package main
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func main() {
s := `"53\'1package main
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func main() {
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
s = strings.ReplaceAll(s, `\'`, `'`)
s2, err := strconv.Unquote(s)
if err != nil {
fmt.Println(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
fmt.Println(err)
}
fmt.Println(u.String())
}
31@265o52package main
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func main() {
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
s = strings.ReplaceAll(s, `\'`, `'`)
s2, err := strconv.Unquote(s)
if err != nil {
fmt.Println(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
fmt.Println(err)
}
fmt.Println(u.String())
}
213"`
s = strings.ReplaceAll(s, `\'`, `'`)
s2, err := strconv.Unquote(s)
if err != nil {
fmt.Println(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
fmt.Println(err)
}
fmt.Println(u.String())
}
英文:
If fails because of \'
. When using backticks, all backslashes are literally backslashes and not escape sequences, so you are passing raw backslash \
, followed by single quote '
to the strconv.Unquote
. It causes invalid syntax. There are two workarounds here:
First
Just replace this line:
s := `"53\'1s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
213"`
with this:
s := `"53'1s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
213"`
So there is '
not \'
. But if you need to programmatically convert the string, use the second approach.
Second
Import "strings"
:
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
And replace \'
with '
:
s = strings.ReplaceAll(s, `\'`, `'`)
So the full code looks like this now:
package main
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func main() {
s := `"53\'1package main
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func main() {
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
s = strings.ReplaceAll(s, `\'`, `'`)
s2, err := strconv.Unquote(s)
if err != nil {
fmt.Println(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
fmt.Println(err)
}
fmt.Println(u.String())
}
31@265o52package main
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
func main() {
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
s = strings.ReplaceAll(s, `\'`, `'`)
s2, err := strconv.Unquote(s)
if err != nil {
fmt.Println(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
fmt.Println(err)
}
fmt.Println(u.String())
}
213"`
s = strings.ReplaceAll(s, `\'`, `'`)
s2, err := strconv.Unquote(s)
if err != nil {
fmt.Println(err)
}
by := []byte(s2)
u, err := uuid.FromBytes(by)
if err != nil {
fmt.Println(err)
}
fmt.Println(u.String())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论