builtin-fmt.go: print() and println() can write data to a generic Writer. They fetch the writer from the control variable '_stdout'.
If _stdout is nil, they write to os.Stdout
This commit is contained in:
parent
cfddbd60b9
commit
ef1baa11a8
@ -4,11 +4,26 @@
|
||||
// builtin-fmt.go
|
||||
package expr
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func getStdout(ctx ExprContext) io.Writer {
|
||||
var w io.Writer
|
||||
if wany, exists := ctx.GetVar(ControlStdout); exists && wany != nil {
|
||||
w, _ = wany.(io.Writer)
|
||||
}
|
||||
if w == nil {
|
||||
w = os.Stdout
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func printFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var n int
|
||||
if n, err = fmt.Print(args...); err == nil {
|
||||
if n, err = fmt.Fprint(getStdout(ctx), args...); err == nil {
|
||||
result = int64(n)
|
||||
}
|
||||
return
|
||||
@ -16,7 +31,7 @@ func printFunc(ctx ExprContext, name string, args []any) (result any, err error)
|
||||
|
||||
func printLnFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var n int
|
||||
if n, err = fmt.Println(args...); err == nil {
|
||||
if n, err = fmt.Fprintln(getStdout(ctx), args...); err == nil {
|
||||
result = int64(n)
|
||||
}
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user