github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/net/file.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import "os"
     8  
     9  type fileAddr string
    10  
    11  func (fileAddr) Network() string  { return "file+net" }
    12  func (f fileAddr) String() string { return string(f) }
    13  
    14  // FileConn returns a copy of the network connection corresponding to
    15  // the open file f.
    16  // It is the caller's responsibility to close f when finished.
    17  // Closing c does not affect f, and closing f does not affect c.
    18  func FileConn(f *os.File) (c Conn, err error) {
    19  	c, err = fileConn(f)
    20  	if err != nil {
    21  		err = &OpError{Op: "file", Net: "file+net", Source: nil, Addr: fileAddr(f.Name()), Err: err}
    22  	}
    23  	return
    24  }
    25  
    26  // FileListener returns a copy of the network listener corresponding
    27  // to the open file f.
    28  // It is the caller's responsibility to close ln when finished.
    29  // Closing ln does not affect f, and closing f does not affect ln.
    30  func FileListener(f *os.File) (ln Listener, err error) {
    31  	ln, err = fileListener(f)
    32  	if err != nil {
    33  		err = &OpError{Op: "file", Net: "file+net", Source: nil, Addr: fileAddr(f.Name()), Err: err}
    34  	}
    35  	return
    36  }
    37  
    38  // FilePacketConn returns a copy of the packet network connection
    39  // corresponding to the open file f.
    40  // It is the caller's responsibility to close f when finished.
    41  // Closing c does not affect f, and closing f does not affect c.
    42  func FilePacketConn(f *os.File) (c PacketConn, err error) {
    43  	c, err = filePacketConn(f)
    44  	if err != nil {
    45  		err = &OpError{Op: "file", Net: "file+net", Source: nil, Addr: fileAddr(f.Name()), Err: err}
    46  	}
    47  	return
    48  }