write func with optional bool return value

1,577 views
Skip to first unread message

Vasiliy Tolstov

unread,
Aug 27, 2021, 10:58:19 AM8/27/21
to golan...@googlegroups.com
Does it possible to have own func that have string, bool return value,
but bool value optional?
Like receiving from channel or get element from map, or like when
casting to some type interface?

--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru

Levieux Michel

unread,
Aug 27, 2021, 11:04:24 AM8/27/21
to Vasiliy Tolstov, golang-nuts
Hi,

Optional returns and parameters don't exist in go, what you are referring to is built-in behavior specific to some features.

A good way to approach what you want is by assigning it to the anonymous '_' var when you don't want the returned bool.

Hope this helps

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CACaajQvoPEGMM-KB3uRmibXr_HL_aqz_bb2%3DKT%3DsbQKR4U4Gkw%40mail.gmail.com.

Vasiliy Tolstov

unread,
Aug 27, 2021, 11:06:58 AM8/27/21
to Levieux Michel, golang-nuts
I know, so my question is - does it possible to write such functions
like builtin? For example I can create func via reflect....

пт, 27 авг. 2021 г. в 18:03, Levieux Michel <mlevi...@gmail.com>:

Axel Wagner

unread,
Aug 27, 2021, 11:19:06 AM8/27/21
to Vasiliy Tolstov, Levieux Michel, golang-nuts
No, it is not possible. The *only* functions that can return either one or two returns are builtins.
That's because the number of returns is part of the type of a function, so it is constant for every function.
Builtin "functions", OTOH, are not really functions, in terms of the language. They are intrinsics of the compiler, which parses the call-expression and translates them into machine code or calls into the runtime (which is also how they can be generic, without Go having generics). You can see that by trying to assign a builtin function to a variable: https://play.golang.org/p/0EiSIIqSLhp
This would be possible for every function - but `append` has no type, so you can't put it in a variable (which would need to have the same type as `append`).

What you ask is just categorically impossible in Go.

Vasiliy Tolstov

unread,
Aug 27, 2021, 11:46:23 AM8/27/21
to Axel Wagner, Vasiliy Tolstov, Levieux Michel, golang-nuts
Ok, thank you for the clarification

пт, 27 авг. 2021 г. в 18:18, Axel Wagner <axel.wa...@googlemail.com>:
Reply all
Reply to author
Forward
0 new messages