Ask about Golang Behavior

73 views
Skip to first unread message

febriananda pramudita

unread,
Mar 20, 2023, 11:44:54 AM3/20/23
to golang-nuts
I was playing with protobuf marshaller recently and found this behavior

https://gist.github.com/FwP-pintu/448f8ecddf27987a7e5b8a103eda12cb

Is behavior: "nil slice with more than 0 capacity" is intended? Thank you.

Tamás Gulácsi

unread,
Mar 20, 2023, 12:15:28 PM3/20/23
to golang-nuts
You should print cap(newMessage.Biawaks).
A nil slice cannot have non-zero capacity.
A nil slice and a zero-length slice is semantically equivalent.

Brian Candler

unread,
Mar 20, 2023, 12:25:48 PM3/20/23
to golang-nuts
On Monday, 20 March 2023 at 16:15:28 UTC Tamás Gulácsi wrote:
You should print cap(newMessage.Biawaks).

Good catch on that typo :-)

FWIW, here it is as a playground link: https://go.dev/play/p/CHaW3lMutXN

jake...@gmail.com

unread,
Mar 20, 2023, 12:26:41 PM3/20/23
to golang-nuts
You have:
fmt.Println("AFTER:", newMessage.Biawaks == nil, cap(message.Biawaks))
Did you mean:
fmt.Println("AFTER:", newMessage.Biawaks == nil, cap( newMessage.Biawaks))

If so, then
Reply all
Reply to author
Forward
0 new messages