Satge #QQ0 some bytes($%d) missed when TCP server read from client in Golang

hello all. I’m stuck on Stage #QQ0. my code has passed course test. But when i run my code locally, i encounter some error:

i want to read bytes from client, but some data missed.

i used nc to send data to my 0.0.0.0:6379:

echo -n "*2\r\n$4\r\nECHO\r\n$9\r\nraspberry\r\n" | nc 0.0.0.0 6379

but my server get data: "*2\r\n\r\nECHO\r\n\r\nraspberry\r\n". $4 and $9 have missed. Does anyone know what’s going on? thanks

this my code on github: codecrafters-redis-go/app/server.go at master · batreeon/codecrafters-redis-go · GitHub

func main() {
	// You can use print statements as follows for debugging, they'll be visible when running tests.
	fmt.Println("Logs from your program will appear here!")

	l, err := net.Listen("tcp", "0.0.0.0:6379")
	if err != nil {
		fmt.Println("Failed to bind to port 6379")
	}
	defer l.Close()

	for { // handle multiple client connections
		conn, err := l.Accept()
		if err != nil {
			fmt.Println("Error accepting connection: ", err.Error())
			os.Exit(1)
		}

		go func() {
			defer conn.Close()
			for { // handle multiple commands in the same connection
				buf := make([]byte, 1024)
				n, err := conn.Read(buf)
				if err != nil {
					fmt.Println("Error read request: ", err.Error())
					return // return when one connection handler completed
				}

				fmt.Println("1 ", string(buf))
                                ......
			}
		}()
	}
}

Ah, I think this due to the variable substitution behaviour of shells. The dollar symbol inside double quotes is a special character used to replace with the contents of a variable.

@batreeon Could you try using single quotes instead? Oh, and another question - did you see double quotes used in our logs/instructions anywhere? If so we’ll get this corrected.

thank you. i got it.

echo "$4"

is equivalent to

echo $4

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