gpt4 book ai didi

file - Golang 添加自定义 os.File 到 os.Stdout

转载 作者:数据小太阳 更新时间:2023-10-29 03:32:25 24 4
gpt4 key购买 nike

每当我向控制台打印内容时,我都试图将输出写入文件。似乎没有任何使用连续流的好例子,而是读取单个值,所以我想出了以下代码:

package main

import (
"fmt"
"io"
"os"
)

type ahhh struct {
*os.File
__writer io.Writer
}

func (me *ahhh) Write(b []byte) (n int, err error) {
return me.__writer.Write(b)
}

func write_print_to_file(file_name string) {
file, _ := os.OpenFile(file_name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)

new_stdout := &ahhh{file, io.MultiWriter(file, os.Stdout)}

os.Stdout = new_stdout
}

func main() {
write_print_to_file("output.log")
fmt.Println("Hello world!")
}

出于某种原因 os.Stdout = new_stdout 不想工作。我相当确定这应该是一个允许的分配,因为我在我的 FUSE 文件系统中使用了类似结构的东西,所以我很好奇为什么 os.Stdout 不想接受我继承的 os.文件

或者,如果这真的不可能,有什么好的建议可以将连续的数据流发送到文件吗?

最佳答案

$ go doc os.stdout
var (
Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
)
Stdin, Stdout, and Stderr are open Files pointing to the standard input,
standard output, and standard error file descriptors.

Note that the Go runtime writes to standard error for panics and crashes;
closing Stderr may cause those messages to go elsewhere, perhaps to a file
opened later.

$ go doc os.newfile
func NewFile(fd uintptr, name string) *File
NewFile returns a new File with the given file descriptor and name. The
returned value will be nil if fd is not a valid file descriptor. On Unix
systems, if the file descriptor is in non-blocking mode, NewFile will
attempt to return a pollable File (one for which the SetDeadline methods
work).

$ go doc -u os.file
type File struct {
*file // os specific
}
File represents an open file descriptor.

func Create(name string) (*File, error)
func NewFile(fd uintptr, name string) *File
func Open(name string) (*File, error)
func OpenFile(name string, flag int, perm FileMode) (*File, error)
func newFile(fd uintptr, name string, kind newFileKind) *File
func openFdAt(fd int, path string) (*File, error)
func openFileNolog(name string, flag int, perm FileMode) (*File, error)
func (f *File) Chdir() error
func (f *File) Chmod(mode FileMode) error
func (f *File) Chown(uid, gid int) error
func (f *File) Close() error
func (f *File) Fd() uintptr
func (f *File) Name() string
func (f *File) Read(b []byte) (n int, err error)
func (f *File) ReadAt(b []byte, off int64) (n int, err error)
func (f *File) Readdir(n int) ([]FileInfo, error)
func (f *File) Readdirnames(n int) (names []string, err error)
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
func (f *File) SetDeadline(t time.Time) error
func (f *File) SetReadDeadline(t time.Time) error
func (f *File) SetWriteDeadline(t time.Time) error
func (f *File) Stat() (FileInfo, error)
func (f *File) Sync() error
func (f *File) SyscallConn() (syscall.RawConn, error)
func (f *File) Truncate(size int64) error
func (f *File) Write(b []byte) (n int, err error)
func (f *File) WriteAt(b []byte, off int64) (n int, err error)
func (f *File) WriteString(s string) (n int, err error)
func (f *File) checkValid(op string) error
func (f *File) chmod(mode FileMode) error
func (file File) close() error
func (f *File) pread(b []byte, off int64) (n int, err error)
func (f *File) pwrite(b []byte, off int64) (n int, err error)
func (f *File) read(b []byte) (n int, err error)
func (f *File) readdir(n int) (fi []FileInfo, err error)
func (f *File) readdirnames(n int) (names []string, err error)
func (f *File) seek(offset int64, whence int) (ret int64, err error)
func (f *File) setDeadline(t time.Time) error
func (f *File) setReadDeadline(t time.Time) error
func (f *File) setWriteDeadline(t time.Time) error
func (f *File) wrapErr(op string, err error) error
func (f *File) write(b []byte) (n int, err error)
type file struct {
pfd poll.FD
name string
dirinfo *dirInfo // nil unless directory being read
nonblock bool // whether we set nonblocking mode
stdoutOrErr bool // whether this is stdout or stderr
}
file is the real representation of *File. The extra level of indirection
ensures that no clients of os can overwrite this data, which could cause the
finalizer to close the wrong file descriptor.

func (file *file) close() error

I'm fairly certain this should be an allowed assignment.

new_stdout := &ahhh{file, io.MultiWriter(file, os.Stdout)}
os.Stdout = new_stdout

它是一个*os.File吗?不。为什么要允许?

关于file - Golang 添加自定义 os.File 到 os.Stdout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54276178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com