Golang add custom os.File to os.Stdout












-1















I'm trying to write output to a file whenever I print something to the console. There didn't seem to be any nice examples out there using a continuous stream, but rather reading a single value so I came up with the following code:



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!")
}


For some reason os.Stdout = new_stdout doesn't want to work. I'm fairly certain this should be an allowed assignment as I use something of similar structure in my FUSE file system, so I'm curious why os.Stdout doesn't want to accept my inherited os.File.



Alternatively if this really isn't possible any good suggestions for making a continuous data stream to a file?










share|improve this question





























    -1















    I'm trying to write output to a file whenever I print something to the console. There didn't seem to be any nice examples out there using a continuous stream, but rather reading a single value so I came up with the following code:



    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!")
    }


    For some reason os.Stdout = new_stdout doesn't want to work. I'm fairly certain this should be an allowed assignment as I use something of similar structure in my FUSE file system, so I'm curious why os.Stdout doesn't want to accept my inherited os.File.



    Alternatively if this really isn't possible any good suggestions for making a continuous data stream to a file?










    share|improve this question



























      -1












      -1








      -1


      1






      I'm trying to write output to a file whenever I print something to the console. There didn't seem to be any nice examples out there using a continuous stream, but rather reading a single value so I came up with the following code:



      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!")
      }


      For some reason os.Stdout = new_stdout doesn't want to work. I'm fairly certain this should be an allowed assignment as I use something of similar structure in my FUSE file system, so I'm curious why os.Stdout doesn't want to accept my inherited os.File.



      Alternatively if this really isn't possible any good suggestions for making a continuous data stream to a file?










      share|improve this question
















      I'm trying to write output to a file whenever I print something to the console. There didn't seem to be any nice examples out there using a continuous stream, but rather reading a single value so I came up with the following code:



      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!")
      }


      For some reason os.Stdout = new_stdout doesn't want to work. I'm fairly certain this should be an allowed assignment as I use something of similar structure in my FUSE file system, so I'm curious why os.Stdout doesn't want to accept my inherited os.File.



      Alternatively if this really isn't possible any good suggestions for making a continuous data stream to a file?







      file go io stdout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 13:34









      A.R

      2,2152922




      2,2152922










      asked Jan 20 at 11:58









      3rdaccountQQ3rdaccountQQ

      9610




      9610
























          1 Answer
          1






          active

          oldest

          votes


















          2















          $ 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



          Is it an *os.File? No. Why should it be allowed?






          share|improve this answer


























          • Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

            – 3rdaccountQQ
            Jan 20 at 12:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276178%2fgolang-add-custom-os-file-to-os-stdout%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2















          $ 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



          Is it an *os.File? No. Why should it be allowed?






          share|improve this answer


























          • Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

            – 3rdaccountQQ
            Jan 20 at 12:15
















          2















          $ 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



          Is it an *os.File? No. Why should it be allowed?






          share|improve this answer


























          • Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

            – 3rdaccountQQ
            Jan 20 at 12:15














          2












          2








          2








          $ 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



          Is it an *os.File? No. Why should it be allowed?






          share|improve this answer
















          $ 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



          Is it an *os.File? No. Why should it be allowed?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 12:15

























          answered Jan 20 at 12:04









          peterSOpeterSO

          94.9k14160175




          94.9k14160175













          • Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

            – 3rdaccountQQ
            Jan 20 at 12:15



















          • Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

            – 3rdaccountQQ
            Jan 20 at 12:15

















          Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

          – 3rdaccountQQ
          Jan 20 at 12:15





          Oh yeah, I guess os.File would need to be a interface and not a struct for this to work

          – 3rdaccountQQ
          Jan 20 at 12:15




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276178%2fgolang-add-custom-os-file-to-os-stdout%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre