10

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

What does this 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) } 

Thank you.

1 Answer 1

14

The message is confusing, until you realize that the ... is the variadic portion of a C function. You can't use C variadic functions directly from Go, so you'll have to write a small wrapper in C to call sem_open.

A couple more notes:

  • C.free should be called with C.free(unsafe.Pointer(name))
  • val needs to be a *C.int
  • sem_getvalue uses errno, so you should call it with ret, err := C.sem_getvalue...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.