package stdio
import (
"bufio"
"bytes"
"io"
"os"
"strings"
)
func DiscardReader (src io .Reader ) {
_, _ = io .Copy (io .Discard , src )
}
func ReadString (r io .Reader ) string {
bs , err := io .ReadAll (r )
if err != nil {
return ""
}
return string (bs )
}
func MustReadReader (r io .Reader ) []byte {
bs , err := io .ReadAll (r )
if err != nil {
panic (err )
}
return bs
}
func NewIOReader (in any ) io .Reader {
switch typIn := in .(type ) {
case []byte :
return bytes .NewReader (typIn )
case string :
return strings .NewReader (typIn )
case io .Reader :
return typIn
}
panic ("invalid input type for create reader" )
}
func NewScanner (in any ) *bufio .Scanner {
switch typIn := in .(type ) {
case io .Reader :
return bufio .NewScanner (typIn )
case []byte :
return bufio .NewScanner (bytes .NewReader (typIn ))
case string :
return bufio .NewScanner (strings .NewReader (typIn ))
case *bufio .Scanner :
return typIn
default :
panic ("invalid input type for create scanner" )
}
}
func SafeClose (c io .Closer ) {
_ = c .Close ()
}
func WriteByte (b byte ) {
_, _ = os .Stdout .Write ([]byte {b })
}
func WriteBytes (bs []byte ) {
_, _ = os .Stdout .Write (bs )
}
func WritelnBytes (bs []byte ) {
_, _ = os .Stdout .Write (bs )
_, _ = os .Stdout .Write ([]byte ("\n" ))
}
func WriteString (s string ) {
_, _ = os .Stdout .WriteString (s )
}
func Writeln (s string ) {
_, _ = os .Stdout .WriteString (s )
_, _ = os .Stdout .Write ([]byte ("\n" ))
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .