linode / linode/docs

Create a TCP and UDP Client and Server using Go Proposed Changes

Open
#3,388 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.4k
Forks
1.2k
Avg merge
9m
Merged PRs (30d)
1

Description

Link: https://linode.com/docs/development/go/developing-udp-and-tcp-clients-and-servers-in-go/

Issue

The example programs create and discard bufio.Readers in inner loops. Any data buffered in the bufio.Reader is also discarded. The discarded data may not be an issue in your tests cases, but people are copying your code to their own applications where things break.

This error is common in network programming questions on StackOverflow. I suspect that many of the questioners are getting their code from this page.

Suggested Fix

Change code like this:

   for {
            reader := bufio.NewReader(os.Stdin)
            fmt.Print(">> ")
            text, _ := reader.ReadString('\n')
            fmt.Fprintf(c, text+"\n")

            message, _ := bufio.NewReader(c).ReadString('\n')
            fmt.Print("->: " + message)
            if strings.TrimSpace(string(text)) == "STOP" {
                    fmt.Println("TCP client exiting...")
                    return
            }
    }

To

   inReader := bufio.NewReader(os.Stdin)
   cReader := bufio.NewReader(c)
   for {
            fmt.Print(">> ")
            text, _ := inReader.ReadString('\n')
            fmt.Fprintf(c, text+"\n")

            message, _ := cReader.ReadString('\n')
            fmt.Print("->: " + message)
            if strings.TrimSpace(string(text)) == "STOP" {
                    fmt.Println("TCP client exiting...")
                    return
            }
    }

All uses of bufio.NewReader in the example programs have this problem. Fix all of them.

Hall of Shame (SO questions that follow the bad advice in the Linode article):

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Go TCP and UDP clients and servers article and inspect every example using bufio.NewReader. Keep readers reused across inner loops as shown in the issue, then verify that all example programs no longer recreate readers inside those loops.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
documentation, networking
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.