Skip to content

Pass arguments to subscriber callback #42

Answered by aler9
joao-aguizo asked this question in Q&A
Discussion options

You must be logged in to vote

Hello, one of the advantages of Golang with respect to Python is that it supports anonymous functions, therefore, you can add as many arguments as you want to the callback function, in this way

package main

import (
	"fmt"

	"github.com/aler9/goroslib"
	"github.com/aler9/goroslib/pkg/msgs/sensor_msgs"
)

func onMessage(msg *sensor_msgs.Imu, topicName string) {
	fmt.Printf("Incoming: %+v\n", msg)
        fmt.Println("topic name:", topicName)
}

func main() {
	// create a node and connect to the master
	n, err := goroslib.NewNode(goroslib.NodeConf{
		Name:          "goroslib_sub",
		MasterAddress: "127.0.0.1:11311",
	})
	if err != nil {
		panic(err)
	}
	defer n.Close()

	// create a subscr…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by joao-aguizo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #41 on April 03, 2021 16:04.