The CGOEmitter package serves to obtain asynchronous C code response returns, facilitating the process of returning the data returned to the program in GO.
You must include the cgoemitter.h file to obtain the required methods.
File: (github.com/user/packagename/x/x.h
)
#ifndef X_H_
#define X_H_
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "../../cgoemitter.h"
void say_message();
#endif
File: (github.com/user/packagename/x/x.c
)
#include "x.h"
void check_err_cgoemitter_args_halloc_arg(void* value) {
if (value == NULL) puts("Failed on cgoemitter_args_halloc_arg()");
}
void check_err_cgoemitter_args_add_arg(int code) {
if (code == EXIT_FAILURE) puts("Failed on cgoemitter_args_add_arg()");
}
void say_message() {
char* message = "Parameter sent from C language";
cgoemitter_args_t cgoemitter_args = cgoemitter_new_args(1);
void* message_arg = cgoemitter_args_halloc_arg(&message, (strlen(message)+1) * sizeof(char));
check_err_cgoemitter_args_halloc_arg(message_arg);
check_err_cgoemitter_args_add_arg(cgoemitter_args_add_arg(&cgoemitter_args, &message_arg));
emit("message", &cgoemitter_args);
}
It is extremely important to add the flags below so that your code is compiled.
#cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
#cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
File: (github.com/user/packagename/x/x.go
)
package x
// The LDFLAGS lines below are needed to prevent linker errors
// since not all packages are present while building intermediate
// packages. The darwin build tag is used as a proxy for clang
// versus gcc because there doesn't seem to be a better way
// to detect this.
/*
#cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
#cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
#include "x.h"
*/
import "C"
//SayMessage | Execute C function
func SayMessage() {
C.say_message()
}
File: (github.com/user/packagename/main.go
)
package main
import (
"fmt"
"sync"
"github.com/supermock/cgoemitter"
"github.com/user/packagename/x"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
cgoemitter.On("message", cgoemitter.NewListener(func(args cgoemitter.Arguments) {
fmt.Printf("Receveid message: %s\n", args.String(0))
wg.Done()
}))
x.SayMessage()
wg.Wait()
}
$ go run main.go
These warnings help you identify issues with your implementation, such as events triggered without manipulators.
Example:
cgoemitter.On("cgoemitter-warnings", cgoemitter.NewListener(func(args cgoemitter.Arguments) {
fmt.Println("[WARNING]", args.String(0))
}))
- On() => Add a new listener to the event.
- Off() => Removes an existing listener in the event.
- Once() => Add a new listener to the event with just one shot.
- NewListener() => Creates a new listener.
- GetListeners() => Returns all listeners to an event.
parser
/CStructToGoStruct() => Transports data from a structure received from C to a structure in the GO.
From this version onwards a security measure was applied when using CGO, stay inside so you do not have problems.
For more details see:
If you have a problem compiling on account of an invalid flag
warning. In the commands below, you allow the use of any flag, just for console session. (Not recommended, only for trusted code)
$ export CGO_CFLAGS_ALLOW='^.*\S'
$ export CGO_LDFLAGS_ALLOW='^.*\S'
$ go run main.go
- Add new conversions of types from C to GO in Arguments.
- Add new treatments from C types to the GO in the CStructToGoStruct method
Clique aqui e leia em português
Just download the code make your change and send a pull request explaining the purpose if it is a bug or an improvement and etc... After this will be analyzed to be approved. Note: If it is a major change, open a issue explaining what will be done so you do not waste your precious time developing something that will not be used. Make yourself at home!
MIT