How to convert/assign a value with type *string to type *wrapperspb.StringValue when the *string value is nil?

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

How to convert/assign a value with type *string to type *wrapperspb.StringValue when the *string value is nil?

问题

我正在尝试将类型为*string的值分配给类型为*wrapperspb.StringValue的变量。然而,当*string为nil时,会触发错误(请参见代码片段中的注释以了解错误的类型)。

以下是我代码的简化版本:

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"

	"google.golang.org/protobuf/types/known/wrapperspb"
)

func main() {
	var var1 *wrapperspb.StringValue
	var1 = &wrapperspb.StringValue{Value: "test1"}
	fmt.Println("var1:")
	fmt.Println(var1)

	var var2 *string
	fmt.Println("var2:")
	fmt.Println(var2)

	//var1 = var2 // it says "Cannot use 'var2' (type *string) as the type *wrapperspb.StringValue"
	//var1 = wrapperspb.String(*var2) // it says panic: runtime error: invalid memory address or nil pointer dereference
	//fmt.Println("var1 with var2 value:")
	//fmt.Println(var1)
}

有人知道如何正确处理转换/赋值吗?

这是一个golang playground链接:https://go.dev/play/p/5JBfU0oEIC-

英文:

I am trying to assign a value with type *string to a variable with type *wrapperspb.StringValue. However, when the *string is nil, it triggers an error (please see the comments in the snipped code to see what kind of error).

Here is a simplified version of my code:

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"

	"google.golang.org/protobuf/types/known/wrapperspb"
)

func main() {
	var var1 *wrapperspb.StringValue
	var1 = &wrapperspb.StringValue{Value: "test1"}
	fmt.Println("var1:")
	fmt.Println(var1)

	var var2 *string
	fmt.Println("var2:")
	fmt.Println(var2)

	//var1 = var2 // it says "Cannot use 'var2' (type *string) as the type *wrapperspb.StringValue"
	//var1 = wrapperspb.String(*var2) // it says panic: runtime error: invalid memory address or nil pointer dereference
	//fmt.Println("var1 with var2 value:")
	//fmt.Println(var1)
}

Does anyone know how to properly handle the conversion/assignment?

Here is a golang playground: https://go.dev/play/p/5JBfU0oEIC-

答案1

得分: 2

如果你的var2字符串指针是nil,你也应该将var1*wrapperspb.StringValue指针设置为nilwrapperspb.StringValue的方法会处理自身是否为nil指针的情况。所以可以像这样进行转换:

if var2 == nil {
    var1 = nil
} else {
    var1 = wrapperspb.String(*var2)
}

进行测试:

for i := 0; i < 2; i++ {
    var var1 *wrapperspb.StringValue

    var var2 *string
    if i == 0 {
        s := "test"
        var2 = &s
    }

    if var2 == nil {
        var1 = nil
    } else {
        var1 = wrapperspb.String(*var2)
    }
    fmt.Printf("var2: %v\n", var2)
    fmt.Printf("var1: %v\n", var1)
    fmt.Printf("%q\n", var1.GetValue())
}

这将输出(在Go Playground上尝试):

var2: 0xc00009e470
var1: value:"test"
"test"
var2: <nil>
var1: <nil>
""
英文:

If your var2 string pointer is nil, you should also leave the var1 *wrapperspb.StringValue pointer nil as well. Methods of wrapperspb.StringValue handles if itself is the nil pointer. So "convert" it like this:

if var2 == nil {
    var1 = nil
} else {
    var1 = wrapperspb.String(*var2)
}

Testing it:

for i := 0; i &lt; 2; i++ {
	var var1 *wrapperspb.StringValue

	var var2 *string
	if i == 0 {
		s := &quot;test&quot;
		var2 = &amp;s
	}

	if var2 == nil {
		var1 = nil
	} else {
		var1 = wrapperspb.String(*var2)
	}
	fmt.Printf(&quot;var2: %v\n&quot;, var2)
	fmt.Printf(&quot;var1: %v\n&quot;, var1)
	fmt.Printf(&quot;%q\n&quot;, var1.GetValue())
}

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

var2: 0xc00009e470
var1: value:&quot;test&quot;
&quot;test&quot;
var2: &lt;nil&gt;
var1: &lt;nil&gt;
&quot;&quot;

huangapple
  • 本文由 发表于 2022年3月17日 01:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71501471.html
匿名

发表评论

匿名网友

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

确定