I don't know about callbacks in Golang

3,868 views
Skip to first unread message

Eduardo Moseis Fuentes

unread,
May 4, 2018, 8:53:13 PM5/4/18
to golang-nuts
HI everyone I´m Eduardo from Guatemala and I'm beginer. I'm  interesting in all scope golang in fact  I was download a little book about it, but I need learn more about callbacks because the book don´t has enough information on callbacks. May somebody  tell me where can I  find more information?. HELP ME PLEASE  THANKS God Bless you

Krzysztof Kowalczyk

unread,
May 5, 2018, 12:05:07 AM5/5/18
to golang-nuts

Justin Israel

unread,
May 5, 2018, 5:56:40 PM5/5/18
to Eduardo Moseis Fuentes, golang-nuts
Can you explain what problem you actually want to solve by learning about callbacks? Maybe there is a better learning reference to offer you based on your actual goals. 

Justin 


On Sat, May 5, 2018, 12:52 PM Eduardo Moseis Fuentes <edui...@gmail.com> wrote:
HI everyone I´m Eduardo from Guatemala and I'm beginer. I'm  interesting in all scope golang in fact  I was download a little book about it, but I need learn more about callbacks because the book don´t has enough information on callbacks. May somebody  tell me where can I  find more information?. HELP ME PLEASE  THANKS God Bless you

--
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.
For more options, visit https://groups.google.com/d/optout.

Sokolov Yura

unread,
May 6, 2018, 1:36:07 AM5/6/18
to golang-nuts
Callbacks are rarely used in Go's ecosystem. Most of time you will use callbacks only in your own libraries (if you put them in).
Unnamed functions are as simole as `callIt(func (arg Type, arg2 Type2) (res1 Type, res2 Type) { res1, res2 = body(); return res1, res2}) `

Justin Israel

unread,
May 6, 2018, 2:56:28 AM5/6/18
to Eduardo Moseis Fuentes, golang-nuts
Callbacks are not something that is considered a standard aspect to Go programming, in the same sense as you might see them in something like js/Node.js async programming. The fact that you mentioned Web programming explains why you might have been exposed to the concept, when it is describing your http handlers (function mapped to urls like  "/hello"). So it is likely that you won't see too much specific training material on the topic of "callbacks". But if you are interested in web services, you could take a look at some of the tutorials on the curated awesome-go site: https://github.com/avelino/awesome-go#tutorials

If you just want to see a generic example of passing functions as arguments, to be used as callbacks:

Justin


On Sun, May 6, 2018 at 12:49 PM Eduardo Moseis Fuentes <edui...@gmail.com> wrote:
Thank you to take time to respons me. Specifily I don't have a problem to solve in fact the problem is I don't know how the callbacks works. Well my goal is to learn golang and all its semantic and after that focus my time on  how to program in backend with go.. but I need to learn its semantic before,  and my book's name is " Learning-Go-latest " it has a topic named callbacks on page 29. I will look more references to learn callbacks or golang in general. Do you know about some reference online or offline maybe another book wich always is oriented on develop in the scope web Do you know if someone would like to have an apprentice?.of before hand thanks. God Bless you

sorry by my bad english but I'am lerning too

Juliusz Chroboczek

unread,
May 6, 2018, 5:51:54 PM5/6/18
to golan...@googlegroups.com
> Callbacks are rarely used in Go's ecosystem.

https://golang.org/pkg/sort/#Slice
https://golang.org/pkg/sync/#Map.Range

Sokolov Yura

unread,
May 7, 2018, 2:07:29 AM5/7/18
to golang-nuts
There is semantic difference between callback passed for continuation of asynchronous action, and closure/function passed as algorithm parameter.

sort.Slice and sync.Map.Range both accepts function/closure as algorithm parameter, not as callback.

Louki Sumirniy

unread,
May 7, 2018, 3:03:35 AM5/7/18
to golang-nuts
To use callbacks in Go you must follow Functional Programming rules about shared data. In simple terms, you cannot share data. You can pass pointers to shared data structures, and likely will have to but as soon as you start using also goroutines you will end up with race conditions. To solve this problem the best way usually will be to create a collection of functions that isolate this state data from callers and ensures that changes to it are not clobbered by other threads.

It is not outside the scope of Go idiom to use callbacks, as you may be familiar with the laws of Go - don't share state to communicate, communicate to share state.

Linker

unread,
May 7, 2018, 6:32:41 AM5/7/18
to Louki Sumirniy, golang-nuts
Callback ---- let user to handle context
Coroutine ---- let runtime to handle context

--
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards,
Linker Lin

linker...@gmail.com

Jan Mercl

unread,
May 7, 2018, 6:38:35 AM5/7/18
to Eduardo Moseis Fuentes, golang-nuts
On Sat, May 5, 2018 at 2:52 AM Eduardo Moseis Fuentes <edui...@gmail.com> wrote:

> HI everyone I´m Eduardo from Guatemala and I'm beginer. I'm interesting in all scope golang in fact I was download a little book about it, but I need learn more about callbacks because the book don´t has > enough information on callbacks. May somebody tell me where can I find more information?. HELP ME PLEASE THANKS God Bless you 

Callback is most often used just as fancy term for having/passing around a variable of a function type. Note that both the syntax and semantics of `expr(args)' is the same regardless of expr being simply a name of a function or any other expression of a function type.

--

-j

florent giraud

unread,
May 7, 2018, 10:15:01 AM5/7/18
to Louki Sumirniy, golang-nuts
Hello louki. Can you give us a little example about what you mean. I don't really understand this sentence for me "don't share state to communicate, communicate to share state."

Thanks a lot for all your answears

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

matthe...@gmail.com

unread,
May 7, 2018, 11:08:27 AM5/7/18
to golang-nuts
Callbacks in Go can be done with a func argument to a func, or a similar effect can be made with channels by triggering a callback action by waiting on a blocking channel in the application. This Wikipedia article describes the pattern: https://en.wikipedia.org/wiki/Callback_(computer_programming)

// this func executes callback at some point
// you can specify any func signature for callback when you construct your own func that calls back
func
CallsBack(arg1 int, arg2 string, callback func())

// this func sends a signal on callback that you listen for on another goroutine to execute your callback action
// SignalsCallback may block until callback is read by you, or if the chan is buffered it may continue without callback being read
func
SignalsCallback(arg1 int, arg2 string, callback <-chan struct{})

The difference is that CallsBack will execute callback in order, while SignalsCallback will continue concurrently after callback is read by your goroutine.

Matt

matthe...@gmail.com

unread,
May 7, 2018, 11:25:16 AM5/7/18
to golang-nuts
Corrected mistake:

func SignalsCallback(arg1 int, arg2 string, callback chan<- struct{})

SignalsCallback will only write to callback, not read.

Matt

florent giraud

unread,
May 7, 2018, 12:48:20 PM5/7/18
to matthe...@gmail.com, golang-nuts
ok matthew so what you propose is sync method callback right ?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

matthe...@gmail.com

unread,
May 7, 2018, 1:44:04 PM5/7/18
to golang-nuts
The first approach with a func argument to a func can be synchronous (which is what I was thinking at the time) or it could be asynchronous by using the go keyword on the callback.

Matt
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Eduardo Moseis Fuentes

unread,
May 13, 2018, 3:00:59 PM5/13/18
to golang-nuts
thanks it was useful


Reply all
Reply to author
Forward
0 new messages