Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

func (*TCPConn) Read update the bytes value even though argument is not pointer

My understanding of Golang method/function argument is that it works by call by value. But I came across this method call func (c *TCPConn) Read(b []byte) (int, error) where the argument is non pointer but it updates the variable value in caller. I think I am missing something. Pls help to understand this case.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It depends on the type of that argument. []byte is a slice and slices are actually pointers. Maps are also pointers so consider it when you use slice and Maps in your functions.

https://go.dev/blog/slices-intro:

A slice is a descriptor of an array segment. It consists of a pointer
to the array, the length of the segment, and its capacity (the maximum
length of the segment).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading