Go – 将原始字节字符串转换为 UUID

huangapple go评论80阅读模式
英文:

Go - Convert raw byte string into uuid

问题

我正在尝试将原始字节字符串转换为UUID。以下是我的程序:

案例1:

package main

import (
	"fmt"
	"strconv"

	"github.com/google/uuid"
)

func main() {
	s := `"04
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())
}
2155O~4667c5
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())
}
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\'1
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())
}
31@265o52
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())
}
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 := `"04
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())
}
2155O~466\"7c5
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())
}
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\'1
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())
}
31@265o52
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())
}
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'1
s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52
s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
213"`

而不是:

s := `"53\'1
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52
s := `"\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\'1
package 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@265o52
package 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\'1
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52
s := `"\235\273\'\021\003\261@\022\226\275o\265\322\002\211\263"`
213"`

with this:

s := `"53'1
s := `"\235\273'\021\003\261@\022\226\275o\265\322\002\211\263"`
31@265o52
s := `"\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\'1
package 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@265o52
package 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()) }

huangapple
  • 本文由 发表于 2022年6月11日 14:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/72582188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定