英文:
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
指针设置为nil
。wrapperspb.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 < 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())
}
This will output (try it on the Go Playground):
var2: 0xc00009e470
var1: value:"test"
"test"
var2: <nil>
var1: <nil>
""
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论