string 如何在 Go 中返回 Nil 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52255683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:39:26  来源:igfitidea点击:

How to Return Nil String in Go?

stringgonull

提问by sotona

I have a function which returns a string under certain circumstances, namely when the program runs in Linux or MacOS, otherwise the return value should be nil in order to omit some OS-specific checks further in code.

我有一个函数在某些情况下返回一个字符串,即当程序在 Linux 或 MacOS 中运行时,否则返回值应该为零,以便在代码中进一步省略一些特定于操作系统的检查。

func test() (response string) {
    if runtime.GOOS != "linux" {  
        return nil
    } else {
        /* blablabla*/
    }
}

however when I try to compile this code I get an error:

但是,当我尝试编译此代码时,出现错误:

test.go:10:3: cannot use nil as type string in return argument.

test.go:10:3:不能在返回参数中使用 nil 作为类型字符串。

If I return just an empty string like return "", I cannot compare this return value with nilfurther in code.

如果我只返回一个像 一样的空字符串return "",我就无法将此返回值与nil代码中的其他值进行比较。

So the question is how to return a correct nil string value?

所以问题是如何返回一个正确的 nil 字符串值?

Thank you.

谢谢你。

回答by icza

If you can't use "", return a pointer of type *string; or–since this is Go–you may declare multiple return values, such as: (response string, ok bool).

如果不能使用"",则返回一个类型为 的指针*string;或者——因为这是 Go——你可以声明多个返回值,例如:(response string, ok bool)

Using *string: return nilpointer when you don't have a "useful" string to return. When you do, assign it to a local variable, and return its address.

当您没有要返回的“有用”字符串时,使用*string: 返回nil指针。当你这样做时,将它分配给一个局部变量,并返回它的地址。

func test() (response *string) {
    if runtime.GOOS != "linux" {
        return nil
    } else {
        ret := "useful"
        return &ret
    }
}

Using multiple return values: when you have a useful string to return, return it with ok = true, e.g.:

使用多个返回值:当你有一个有用的字符串要返回时,用 返回它ok = true,例如:

return "useful", true

Otherwise:

除此以外:

return "", false

This is how it would look like:

这是它的样子:

func test() (response string, ok bool) {
    if runtime.GOOS != "linux" {
        return "", false
    } else {
        return "useful", true
    }
}

At the caller, first check the okreturn value. If that's true, you may use the stringvalue. Otherwise, consider it useless.

在调用方,首先检查ok返回值。如果是true,您可以使用该string值。否则,认为它没有用。

Also see related questions:

另见相关问题:

How do I represent an Optional String in Go?

如何在 Go 中表示可选字符串?

Alternatives for obtaining and returning a pointer to string: How do I do a literal *int64 in Go?

获取和返回指针的替代方法string如何在 Go 中执行文字 *int64?

回答by Nicholas

Go has built-in support for multiple return values:

Go 内置了对多个返回值的支持

This feature is used often in idiomatic Go, for exampleto return both result and error values from a function.

这个特性经常在惯用的 Go 中使用,例如从函数返回结果和错误值。

In your case it could be like this:

在你的情况下,它可能是这样的:

func test() (response string, err error) {
    if runtime.GOOS != "linux" {  
        return "", nil
    } else {
        /* blablabla*/
    }
}

And then:

进而:

response, err := test()
if err != nil { 
    // Error handling code
    return;
}

// Normal code 

If you want to ignore the error, simply use _:

如果您想忽略错误,只需使用_

response, _ := test()
// Normal code

回答by Mightee

Go allows multiple return types. So use this to your advantage and return an error or any other type. Check this out for more info: http://golangtutorials.blogspot.com/2011/06/return-values-from-go-functions.html?m=1

Go 允许多种返回类型。因此,请充分利用它并返回错误或任何其他类型。查看更多信息:http: //golangtutorials.blogspot.com/2011/06/return-values-from-go-functions.html?m=1