To print the message in quotes, you can use the %q
format specifier (see the fmt docs).
You'll want to convert your buffer to a string and make sure you only use the part of the buffer that contains data (up to reqLen
):
// buf[:reqLen] is a slice of the first reqLen bytes of buf.
// string(...) creates a string from a slice of bytes.
fmt.Printf("Message contents: %q
", string(buf[:reqLen]))
This will print:
Message contents: "message
"
The
is inserted by echo
. If you don't want it, run echo -n ...
, or strip surrounding whitespace/newlines using strings.TrimSpace.