tinygo-org / drivers

TinyGo drivers for sensors, displays, wireless adaptors, and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
604 stars 188 forks source link

Cannot use bufio.Scanner for conn #431

Open sago35 opened 2 years ago

sago35 commented 2 years ago

For now, conn.Read may return a length of 0, so bufio.NewScanner() cannot be used. Similarly, fmt.Fscanf() cannot be used.

Similar to the following https://github.com/tinygo-org/tinygo/pull/2739

What works now is the following code.

    buf := [1024]byte{}
    for {
        n := int(0)
        for {
            n, err = conn.Read(buf[:])
            if err != nil {
                failMessage(err.Error())
            }
            if n > 0 {
                break
            }
            time.Sleep(5 * time.Millisecond)
        }
        fmt.Printf("recv: %q\r\n", string(buf[:n]))
    }

I would like the following to work also Because I think that is Go-way code. Of course, we can check for errors by using conn.Read.

    scanner := bufio.NewScanner(conn)
    for scanner.Scan() {
        fmt.Printf("recv: %q\r\n", scanner.Text())
    }