# \[Golang\] Reading from net.Conn hangs or breaks early

**URL:** <https://forum.codecrafters.io/t/golang-reading-from-net-conn-hangs-or-breaks-early/1573>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [August 31, 2024, 9:00pm UTC](https://forum.codecrafters.io/t/golang-reading-from-net-conn-hangs-or-breaks-early/1573 "2024-08-31T21:00:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vyivrain](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/vyivrain/32/1459_2.png) [@vyivrain](https://forum.codecrafters.io/u/vyivrain)\
**Post date:** [August 31, 2024, 9:00pm UTC](https://forum.codecrafters.io/t/golang-reading-from-net-conn-hangs-or-breaks-early/1573/1 "2024-08-31T21:00:52Z")

</div>

Hi. I’m kinda new to Go and stuck with an issue of hanging net connection or not reading full message from net connection.  
Here’s an example how I do it:

```auto
const connChunkSize int = 1024

func readConnectionMessage(conn net.Conn) string {
	buffer := bytes.NewBuffer(nil)
	for {
		chunk := make([]byte, connChunkSize)
		read, err := conn.Read(chunk)

		if read > 0 {
			buffer.Write(chunk[:read])
		}

		if read == 0 || errors.Is(err, io.EOF) {
			break
		} else if err != nil {
			return err.Error()
		}
	}

	return buffer.String()
}

```

I receive a “$1\n$4\nPING” on the first loop and on the second loop `conn.Read` will hang.  
If I do break if statement when read bytes are `< 1024` like:

```auto
if read < 1024 || errors.Is(err, io.EOF) {
    break
}

```

This will work for the first test case. But on the second test case, which’s doing this command in terminal `echo -e "PING\nPING" | redis-cli`, it’ll read only the first PING command as in the first test case, functionality will respond to 1 command and close the connection.

So the question is, how to properly read all the data from connection and avoid hanging? I’ve tried different ways of implementing reading from connection, but all have the same result.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [September 5, 2024, 6:41pm UTC](https://forum.codecrafters.io/t/golang-reading-from-net-conn-hangs-or-breaks-early/1573/2 "2024-09-05T18:41:32Z")

</div>

Sorry for the delayed response.

The issue actually happens outside `readConnectionMessage`. Your code was already nearly perfect. But why did it hang? @vyivrain

Because the code didn’t try to read the second `ping` after sending back the first `pong`.

To continue reading pings, wrap a `for` loop around the read-eval-write logic:

```auto
func handleConnection(conn net.Conn) {
    defer conn.Close()
    for {
        read
        eval
        write
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [September 10, 2024, 6:42pm UTC](https://forum.codecrafters.io/t/golang-reading-from-net-conn-hangs-or-breaks-early/1573/3 "2024-09-10T18:42:02Z")

</div>

Closed on September 10, 2024, 6:42pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
