How to Return Nil String in Go?

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.

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.

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

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

Thank you.

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).

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

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.:

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 ok return value. If that's true, you may use the string value. Otherwise, consider it useless.

Also see related questions:

How do I represent an Optional String in Go?

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting String to URL return nil

From Dev

Go reflect.MakeFunc. How to return a err=nil as reflect.Value?

From Dev

How to return a nil custom UITableViewCell

From Dev

How would I go on as to "return" string arrays between functions?

From Dev

Why can't a string be nil in Go?

From Dev

Convert String to NSURL is return nil in swift

From Dev

Return nil vs zero length string

From Dev

Why does `defined?` return a string or nil?

From Dev

Swift: Nil is incompatible with return type String

From Dev

Read string from a file return nil

From Dev

NSDateFormatter return (null) string when NSDate is nil

From Dev

Return nil vs zero length string

From Dev

Ruby: return the first character from string if it matches and if there is no match then return nil?

From Dev

Ruby: return the first character from string if it matches and if there is no match then return nil?

From Dev

How to check that the string is not nil in swift?

From Dev

How to return nil in swift in a custom initializer?

From Dev

How to find by array and return nil if not matched?

From Dev

Why does NSDateFormatter not return nil when given empty string ("")

From Dev

Rails Model method check if field nil, if so return empty string

From Dev

Why does NSDateFormatter not return nil when given empty string ("")

From Dev

How to return a string?

From Dev

how to return clean string

From Dev

How to pass nil as a param of dictionary<String,AnyObject>

From Dev

How to set optionals that are nil to an empty string?

From Dev

How to check nil string in if condition swift

From Dev

How to check nil string in if condition swift

From Dev

If filter returns nil, how do I not return empty result?

From Dev

How can I return nil if a key does not exist in OpenStruct?

From Dev

how do you return nil if a certain type is passed to a function in Swift?

Related Related

  1. 1

    Converting String to URL return nil

  2. 2

    Go reflect.MakeFunc. How to return a err=nil as reflect.Value?

  3. 3

    How to return a nil custom UITableViewCell

  4. 4

    How would I go on as to "return" string arrays between functions?

  5. 5

    Why can't a string be nil in Go?

  6. 6

    Convert String to NSURL is return nil in swift

  7. 7

    Return nil vs zero length string

  8. 8

    Why does `defined?` return a string or nil?

  9. 9

    Swift: Nil is incompatible with return type String

  10. 10

    Read string from a file return nil

  11. 11

    NSDateFormatter return (null) string when NSDate is nil

  12. 12

    Return nil vs zero length string

  13. 13

    Ruby: return the first character from string if it matches and if there is no match then return nil?

  14. 14

    Ruby: return the first character from string if it matches and if there is no match then return nil?

  15. 15

    How to check that the string is not nil in swift?

  16. 16

    How to return nil in swift in a custom initializer?

  17. 17

    How to find by array and return nil if not matched?

  18. 18

    Why does NSDateFormatter not return nil when given empty string ("")

  19. 19

    Rails Model method check if field nil, if so return empty string

  20. 20

    Why does NSDateFormatter not return nil when given empty string ("")

  21. 21

    How to return a string?

  22. 22

    how to return clean string

  23. 23

    How to pass nil as a param of dictionary<String,AnyObject>

  24. 24

    How to set optionals that are nil to an empty string?

  25. 25

    How to check nil string in if condition swift

  26. 26

    How to check nil string in if condition swift

  27. 27

    If filter returns nil, how do I not return empty result?

  28. 28

    How can I return nil if a key does not exist in OpenStruct?

  29. 29

    how do you return nil if a certain type is passed to a function in Swift?

HotTag

Archive