2

I wonder if there is a way to implement a function that behaves similar to map getter: it returns return value as first argument and (optionally assigned) second value ok as second argument. So I need function f that I can call in following ways:

value1 := f(1)
value2, ok := f(2)

1 Answer 1

1

No, it can't be done, the only option is to return a pointer and check if it's nil.

if v := f(10); v != nil {
     //stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. It's a bit unobvious after using python that some things can be used in standard library but you can't do them by yourself.
there are very few things that you can't do in Go yourself, maybe eventually after they finish porting Go to, well, Go, they might allow that, but probably not gonna happen before 1.6 or so if ever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.