-1

I have the following code from Determine if Stdin has data with Go

package main

import (
    "fmt"
    "os"
)

func main() {
    file := os.Stdin
    fi, err := file.Stat()
    if err != nil {
        fmt.Println("file.Stat()", err)
    }
    size := fi.Size()
    if size > 0 {
        fmt.Printf("%v bytes available in Stdin\n", size)
    } else {
        fmt.Println("Stdin is empty")
    }
}

but no matter how I run it, size is always 0.

I am running Fedora 32 with Go version 1.14

DB Wizz
  • 27
  • 1
  • 1
  • 4

1 Answers1

-1

You have to compile to code to get an executable. Then you can pipe in any text to it, to get its size.

go build pipe.go
ls -lah pipe
2.1M Jul 23 23:06 pipe

Then you can run it by

echo "test" | ./pipe
5 bytes available in Stdin
Saeed
  • 510
  • 1
  • 7
  • 12