Skip to content

Instantly share code, notes, and snippets.

@casimir
Last active August 29, 2015 14:27
Show Gist options
  • Save casimir/922a9a2aa7bfe71b9f3c to your computer and use it in GitHub Desktop.
Save casimir/922a9a2aa7bfe71b9f3c to your computer and use it in GitHub Desktop.
C entrypoint in Go
package main
/*
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef char** args_t;
args_t args_new(int n) {
args_t args = malloc(n * sizeof(char*));
if (args == NULL)
exit(-1);
return args;
}
void args_set(args_t args, int i, char* str) {
args[i] = strdup(str);
}
void args_free(int n, args_t args) {
for (int i = 0; i < n; ++i)
free(args[i]);
free(args);
}
int notmain(int argc, char* argv[]) {
for (int i = 0; i < argc; ++i) {
printf("%d - %s\n", i, argv[i]);
}
return argc;
}
*/
import "C"
import (
"os"
"unsafe"
)
func main() {
argc := C.int(len(os.Args))
argv := C.args_new(argc)
defer C.args_free(argc, argv)
for i, it := range os.Args {
cstr := C.CString(it)
C.args_set(argv, C.int(i), cstr)
C.free(unsafe.Pointer(cstr))
}
os.Exit(int(C.notmain(argc, argv)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment