A short answer is impossible. But since this is not the answer you want to hear, I will give you the right way and the wrong way to solve the problem.
The right way:
- implement
dup() for Windows. - submit to Go to change list
- wait for his release to use it
Obviously, the right way has some problems ... but I highly recommend doing it. Go requires window developers to fix these serious issues. The only reason this cannot be done on Windows is not implemented by the function
Wrong Way:
Until the patch you write is accepted and released, you can fake it using unsafe. How the following code works, mirroring the exact structure of net.UDPConn . This included copying all the structures from the network that make up the UDPConn . Then unsafe used to claim that the local UDPConn matches the UDPConn network. The compiler cannot verify this and takes your word. If the internals of net had ever changed, this would compile, but God knew what he would do.
All code is not verified.
package reallyunsafenet import ( "net" "sync" "syscall" "unsafe" ) // copied from go/src/pkg/net/fd_windows.go type ioResult struct { qty uint32 err error } // copied from go/src/pkg/net/fd_windows.go type netFD struct { // locking/lifetime of sysfd sysmu sync.Mutex sysref int closing bool // immutable until Close sysfd syscall.Handle family int sotype int isConnected bool net string laddr net.Addr raddr net.Addr resultc [2]chan ioResult errnoc [2]chan error // owned by client rdeadline int64 rio sync.Mutex wdeadline int64 wio sync.Mutex } // copied from go/src/pkg/net/udpsock_posix.go type UDPConn struct { fd *netFD } // function to get fd func GetFD(conn *net.UDPConn) syscall.Handle { c := (*UDPConn)(unsafe.Pointer(conn)) return c.fd.sysfd }
Stephen weinberg
source share