英文:
How can I log the value of a pointer if it is not nil, otherwise log nil in GO?
问题
你好,我想要在变量不为nil时记录其值,否则我想要打印其他内容。
例如:
var point *string
var point2 *string
p := "hi"
point2 = &p
fmt.Printf("%v,%v", *point, *point2)
在这种情况下,我会遇到错误,因为point是nil,所以有没有办法在变量不为nil时打印其值,如果为nil则打印其他内容?
我想以一种简单的方式实现这个,而不是创建一个if/else语句来检查变量是否为nil。
英文:
Hi I want to log the value of a variable if it is not nil otherwise I want to print anything else
for example:
var point *string
var point2 *string
p:="hi"
point2=&p
fmt.Printf("%v,%v",*point,*point2)
in this case I will have an error since point is nill, so is there any way to print the value of the variable if it is not nil or print anything else if it is nil?
I want to do this in a simple way instead of creating an if/else statement to check if the variable is nil
答案1
得分: 2
用于记录指针值甚至结构体内部指针值的更通用方法是将其编组为JSON并记录结果。
以下是使用此方法打印单个指针和非指针值以及结构体的示例。
type Line struct {
Point1 *string
Point2 *string
Name string
}
func main() {
point1 := "one"
line := Line{&point1, nil, "fancy"}
printJSON(line.Point1, line.Point2, line.Name)
printJSON(line)
}
func printJSON(i ...interface{}) {
if b, err := json.Marshal(i); err == nil {
log.Println(string(b))
}
}
英文:
For a more general way to log out values of pointers or even pointers within structs you can marshal to JSON and log the results.
Here's an example that prints individual pointer & non-pointer values, as well as a struct using this method.
type Line struct {
Point1 *string
Point2 *string
Name string
}
func main() {
point1 := "one"
line := Line{&point1, nil, "fancy"}
printJSON(line.Point1, line.Point2, line.Name)
printJSON(line)
}
func printJSON(i ...interface{}) {
if b, err := json.Marshal(i); err == nil {
log.Println(string(b))
}
}
答案2
得分: 1
由于Go语言中没有?:
运算符,最好的方法是编写一个函数:
func StringPtrToString(p *string) string {
if p != nil { return *p }
return "(nil)"
}
这个函数接受一个指向字符串的指针作为参数,如果指针不为空,则返回指针指向的字符串;否则返回"(nil)"。
英文:
Since there is no ?:
operator in Go, the best way is to write a function:
func StringPtrToString(p *string) string {
if p != nil { return *p }
return "(nil)"
}
答案3
得分: 1
你应该在这种情况下使用if/else,但是如果对于point
和point2
的每个可能的“if”条件有很多,你可以使用Switch语句。
package main
import (
"fmt"
)
func main() {
type Points struct {
Point *string
Point2 *string
}
p := "hi"
pts := Points{nil, &p}
switch pts.Point {
case nil:
fmt.Printf("%v is empty", pts.Point)
default:
fmt.Printf("%v", *pts.Point)
}
fmt.Println()
switch pts.Point2 {
case nil:
fmt.Printf("%v is empty", pts.Point2)
default:
fmt.Printf("%v", *pts.Point2)
}
}
输出:
<nil> is empty
hi
英文:
You should probably use if/else in this case BUT if you have many potential "if" conditions for each of point
and point2
you can use Switch statements.
package main
import (
"fmt"
)
func main() {
type Points struct {
Point *string
Point2 *string
}
p := "hi"
pts := Points{nil, &p}
switch pts.Point {
case nil:
fmt.Printf("%v is empty", pts.Point)
default:
fmt.Printf("%v", *pts.Point)
}
fmt.Println()
switch pts.Point2 {
case nil:
fmt.Printf("%v is empty", pts.Point2)
default:
fmt.Printf("%v", *pts.Point2)
}
}
Output:
<nil> is empty
hi
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论