Unexpected type: ... with cgo in Go

I am new to Go and trying to learn how to call C from Go. I wrote this program to open a semaphore by name, get the value and print it on the screen. When I run it go build semvalue.go , I get the error: ./semvalue.go:16:14: unexpected type: ...

What does it mean? What am I doing wrong?

 package main import "fmt" // #cgo LDFLAGS: -pthread // #include <stdlib.h> // #include <fcntl.h> // #include <sys/stat.h> // #include <semaphore.h> import "C" func main() { name := C.CString("/fram") defer C.free(name) fram_sem := C.sem_open(name, C.O_CREAT, C.mode_t(0644), C.uint(1)) var val int ret := C.sem_getvalue(fram_sem, val) fmt.Println(val) C.sem_close(fram_sem) } 

Thanks.

+6
source share
1 answer

The message is confusing until you understand that ... is the variational part of function C. You cannot use the variational functions of C directly from Go, so you need to write a small wrapper in C to call sem_open .

A few more notes:

  • C.free should be called using C.free(unsafe.Pointer(name))
  • val must be *C.int
  • sem_getvalue uses errno , so you should call it ret, err := C.sem_getvalue...
+8
source

Source: https://habr.com/ru/post/977995/


All Articles