I'm currently working on a Golang program which reads a 16-bit integer from stdin. I am expecting a value of between 3 and ~128, however, when I print out the integer value it is completely random, going from 30,000+ to -30,000.

I am doing the same thing in PHP by doing the following:

<?php

$binary = fread($stream, 2);
$unpacked = unpack('s', $binary);
$int = reset($unpacked);

This works successfully. The Go code that I am working on is located on GitHub here: https://github.com/uniquoooo/dca/blob/decoding/main.go#L502-L536

cat testfile | command

Here is some testing Golang code:

package main

import (
    "bufio"
    "fmt"
    "encoding/binary"
    "io"
    "os"

    "github.com/layeh/gopus"
)

func main() {
    OpusDecoder, err := gopus.NewDecoder(48000, 2)
    if err != nil {
        panic(err)
    }
    _ = OpusDecoder

    dcabuf := bufio.NewReaderSize(os.Stdin, 16384)

    var opuslen uint16

    for {
        err = binary.Read(dcabuf, binary.LittleEndian, &opuslen)
        if err != nil {
            if err == io.EOF || err == io.ErrUnexpectedEOF {
                return
            }
            panic(err)
        }

        fmt.Println("Frame Size:", opuslen)

        opus := make([]byte, opuslen)
        err = binary.Read(dcabuf, binary.LittleEndian, &opus)
        if err != nil {
            if err == io.EOF || err == io.ErrUnexpectedEOF {
                return
            }
            panic(err)
        }

        // pcm, err := OpusDecoder.Decode(opus, 1920, false)
        // if err != nil {
        //     panic(err)
        // }

        // err = binary.Write(os.Stdout, binary.LittleEndian, &pcm)
        // if err != nil {
        //     if err == io.EOF || err == io.ErrUnexpectedEOF {
        //         return
        //     }
        //     panic(err)
        // }
    }
}
Frame Size: 128
go build
cat test.dca | ./main

The executable name may be different.