Reading a C char array as a Go string #648
Answered
by
mmat11
tamilmani1989
asked this question in
Q&A
-
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.
Answered by
mmat11
Apr 29, 2022
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.
3 replies
Answer selected by
ti-mo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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-.