go lang replace character in value

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

go lang replace character in value

问题

我正在使用下面的脚本片段,"var1"的值返回7d470821-a66a-5d57-b54f-2784ca96c591,现在,我想要移除"var1"中的"-",使其变为7d470821a66a5d57b54f2784ca96c591。我正在尝试以下脚本:

package main

import (
	"fmt"
	"strings"
	"github.com/google/uuid"
)

var namespace = uuid.NameSpaceURL
var value = "abc" + "hello world" + "bow"
var urls1 = []byte(value)

func main() {
	fmt.Println("Generating UUIDs\n----------------")
	generate()
}

func generate() {
	//u, _ := uuid.NewUUID()
	//fmt.Println("UUID version 1:", u)
	var1 := uuid.NewSHA1(namespace, urls1)
	fmt.Println(var1)
	var2 := strings.Replace(var1.String(), "-", "", -1)
	fmt.Println(var2)	
}

但是我得到了以下错误,请帮我纠正错误:

./test-uuid1.go:23:29: cannot use var1 (variable of type uuid.UUID) as string value in argument to strings.Replace
英文:

I am using this below piece of the script, the value of "var1" returns 7d470821-a66a-5d57-b54f-2784ca96c591, now, value that is assigned to "var1", i want to remove "-" so that it becomes 7d470821a66a5d57b54f2784ca96c591. I am trying below script

package main

import (
	"fmt"
	"strings"
	"github.com/google/uuid"
)

var namespace = uuid.NameSpaceURL
var value = "abc" + "hello world" + "bow"
var urls1 = []byte(value)

func main() {
	fmt.Println("Generating UUIDs\n----------------")
	generate()
}

func generate() {
	//u, _ := uuid.NewUUID()
	//fmt.Println("UUID version 1:", u)
	var1 := uuid.NewSHA1(namespace, urls1)
	fmt.Println(var1 )
	var2 = strings.Replace(var1, "-","",0)
	fmt.Println(var2)	
}

and i am getting, please help me to correct what i am wrong.

./test-uuid1.go:23:29: cannot use var1 (variable of type uuid.UUID) as string value in argument to strings.Replace

答案1

得分: 4

strings.Replace() 函数期望一个 string 类型的参数,而 uuid.NewSHA1() 返回一个 uuid.UUID 类型的值。

调用它的 UUID.String() 方法可以获取一个 string 类型的表示。

还要注意,将 0 传递给 strings.Replace() 将执行 0 次替换,因此不会有任何变化。这并不太有用。传递 -1 表示您想要替换所有出现的 -,或者更好的方法是使用 strings.ReplaceAll()

var2 := strings.ReplaceAll(var1.String(), "-", "")

(注意 var2 不存在,所以必须使用 := 短变量声明 而不是简单的 赋值。)

这将输出以下结果(在 Go Playground 上尝试):

生成 UUID
----------------
7d470821-a66a-5d57-b54f-2784ca96c591
7d470821a66a5d57b54f2784ca96c591
英文:

strings.Replace() expects a string, and uuid.NewSHA1() returns a value of type uuid.UUID.

Call its UUID.String() method to obtain a string representation.

Also note that passing 0 to strings.Replace() will perform 0 replaces, so nothing will be changed. Not too useful. Pass -1 to indicate that you want to replace all occurrences of -, or even better, use strings.ReplaceAll():

var2 := strings.ReplaceAll(var1.String(), "-", "")

(Note var2 doesn't exist, so you must use := short variable declaration instead of simple assignment.)

This will output (try it on the Go Playground):

Generating UUIDs
----------------
7d470821-a66a-5d57-b54f-2784ca96c591
7d470821a66a5d57b54f2784ca96c591

答案2

得分: 0

icza的答案解决方案是正确的,已被接受,并且目前有五个赞同票。部分代码如下:

var1 := uuid.NewSHA1(namespace, urls1)
var2 := strings.ReplaceAll(var1.String(), "-", "")

然而,如果你将其作为Go面试问题,你可能更喜欢以下方式:

func UUID5Hex(space uuid.UUID, data []byte) string {
    uuid := uuid.NewSHA1(space, data)
    return hex.EncodeToString(uuid[:])
}

uuid := UUID5Hex(namespace, urls1)

这里是一个示例代码。

面试官可能希望你应用奥卡姆剃刀原理。将UUID格式化为带有连字符的十六进制字符串,然后再移除连字符并不是最简洁的方法。

以下是一些Go基准测试结果:

BenchmarkUUID5Hex-12  3371100  337.4 ns/op  216 B/op  5 allocs/op
BenchmarkUUIDIcza-12  2845947  444.5 ns/op  232 B/op  5 allocs/op

希望对你有帮助!

英文:

icza's answer and solution are correct, it has been accepted, and currently has five upvotes. In part,

var1 := uuid.NewSHA1(namespace, urls1)
var2 := strings.ReplaceAll(var1.String(), "-", "")

However, if you are given this as a Go interview question, you may prefer,

func UUID5Hex(space uuid.UUID, data []byte) string {
    uuid := uuid.NewSHA1(space, data)
    return hex.EncodeToString(uuid[:])
}

uuid := UUID5Hex(namespace, urls1))

https://go.dev/play/p/GJwKZKDTSvp

abchello worldbow
7d470821a66a5d57b54f2784ca96c591

An interviewer may expect you to apply Occam's razor. Formatting the UUID as a hexadecimal string with hyphens and then removing the hyphens is not parsimonious.

Some Go benchmark results:

BenchmarkUUID5Hex-12  3371100  337.4 ns/op  216 B/op  5 allocs/op
BenchmarkUUIDIcza-12  2845947  444.5 ns/op  232 B/op  5 allocs/op

huangapple
  • 本文由 发表于 2023年2月20日 18:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75508243.html
匿名

发表评论

匿名网友

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

确定