Reading a C char array as a Go string #648
-
Hi, My ebpf program gets process name which is retrieved as char array and i use bpf2go to generate types in golang. The golang type converting it as int8[16] and how would i convert that to string in golang?
Generated type in Go code:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You could define your struct in C code as:
which will generate Edit: as Lorenz pointed out below, this field will likely contain a nul-terminated C string sent by the kernel, which can be parsed using https://pkg.go.dev/golang.org/x/sys/unix#ByteSliceToString. See https://go.dev/play/p/16gaV3H2yM-. |
Beta Was this translation helpful? Give feedback.
You could define your struct in C code as:
which will generate
Comm [16]uint8
.Edit: as Lorenz pointed out below, this field will likely contain a nul-terminated C string sent by the kernel, which can be parsed using https://pkg.go.dev/golang.org/x/sys/unix#ByteSliceToString. See https://go.dev/play/p/16gaV3H2yM-.