How to use switch case with nullable string (*string) in Golang?

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

How to use switch case with nullable string (*string) in Golang?

问题

这是我的代码:

func ToSomething(arg *string) string {
    switch arg {
    case nil:
        return "something1"
    case "args1":
        return "something2"
    case "args2":
        return "something3"
    default:
        return "something4"
    }
}

args1args2下面显示了一条红线,上面写着:

argswitch语句中,无效的case'args1'(类型不匹配:'string'和'*string')

有人知道如何在golang中正确使用可空字符串(*string)的switch语句吗?

这是一个Go Playground链接:https://go.dev/play/p/0TaeXSEIt06

英文:

Here is my code:

func ToSomething(arg *string) string {
	switch arg {
	case nil:
		return "something1"
	case "args1":
		return "something2"
	case "args2":
		return "something3"
	default:
		return "something4"
	}
}

It shows a red line under args1 and args2 that says

> Invalid case '"args1"' in switch on 'arg' (mismatched types 'string'
> and '*string')

anyone knows to use switch case with nullable string (*string) properly in golang?

Here is a go playground: https://go.dev/play/p/0TaeXSEIt06

答案1

得分: 3

由于arg的类型是*string,你需要在case分支中列出*string的值。

但是!显然你想要匹配指向的string值,所以列出*string的值并不是你想要的:那样只会检查指针的相等性。

所以,你不应该将arg作为switch表达式,而是在case分支中提供合理的条件,像这样:

func ToSomething(arg *string) string {
    switch {
    case arg == nil:
        return "something1"
    case *arg == "args1":
        return "something2"
    case *arg == "args2":
        return "something3"
    default:
        return "something4"
    }
}

进行测试:

ptr := func(s string) *string { return &s }

fmt.Println(ToSomething(nil))
fmt.Println(ToSomething(ptr("args1")))
fmt.Println(ToSomething(ptr("args2")))
fmt.Println(ToSomething(ptr("xx")))

输出结果(在Go Playground上尝试):

something1
something2
something3
something4
英文:

Since arg is of type *string, you'd have to list values of *string in the case branches.

But! You obviously want to match the pointed string values, so listing *string values is not what you want: that checks for pointer equality.

So instead you should not use arg as the switch expression, but provide sensible conditions on the case branches like this:

func ToSomething(arg *string) string {
	switch {
	case arg == nil:
		return "something1"
	case *arg == "args1":
		return "something2"
	case *arg == "args2":
		return "something3"
	default:
		return "something4"
	}
}

Testing it:

ptr := func(s string) *string { return &s }

fmt.Println(ToSomething(nil))
fmt.Println(ToSomething(ptr("args1")))
fmt.Println(ToSomething(ptr("args2")))
fmt.Println(ToSomething(ptr("xx")))

Output (try it on the Go Playground):

something1
something2
something3
something4

答案2

得分: 2

每个case必须与arg的类型相同。在你的例子中,arg*string类型,而每个case是string类型。

你可以对arg进行解引用操作:

func ToSomething(arg *string) string {
    if arg == nil {
        return "something1"
    }
    switch *arg {
    case "args1":
        return "something2"
    case "args2":
        return "something3"
    default:
        return "something4"
    }
}

这是playground链接。

英文:

Each case must be the same type as arg. In your example, arg is *string and each case is string.

You could dereference arg:

func ToSomething(arg *string) string {
    if arg == nil {
        return "something1"
    }
    switch *arg {
    case "args1":
        return "something2"
    case "args2":
        return "something3"
    default:
        return "something4"
    }
}

Here is the playground.

huangapple
  • 本文由 发表于 2022年3月16日 23:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71499537.html
匿名

发表评论

匿名网友

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

确定