Standard library / net

net.UDPConn.SyscallConn()

Obtain a RawConn for controlled low-level descriptor operations. Use the descriptor only during the callback; prefer portable net methods. This Linux example reads the socket type without changing it. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (c *UDPConn) SyscallConn() (syscall.RawConn, error)

Return Value

(syscall.RawConn, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net"  "syscall"  "time")
func main() {  c, s := udpPair()  defer c.Close()  defer s.Close()  raw := must(s.SyscallConn())  var v int  var e error  check(raw.Control(func(fd uintptr) { v, e = syscall.GetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_TYPE) }))  check(e)  fmt.Println(v == syscall.SOCK_DGRAM)}func tcpListener() *net.TCPListener {  l := must(net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}))  check(l.SetDeadline(time.Now().Add(3 * time.Second)))  return l}func tcpPair() (*net.TCPConn, *net.TCPConn) {  l := tcpListener()  defer l.Close()  a := must(net.DialTCP("tcp4", nil, l.Addr().(*net.TCPAddr)))  b := must(l.AcceptTCP())  check(a.SetDeadline(time.Now().Add(3 * time.Second)))  check(b.SetDeadline(time.Now().Add(3 * time.Second)))  return a, b}func udpListener() *net.UDPConn {  c := must(net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}))  check(c.SetDeadline(time.Now().Add(3 * time.Second)))  return c}func udpPair() (*net.UDPConn, *net.UDPConn) {  b := udpListener()  a := must(net.DialUDP("udp4", nil, b.LocalAddr().(*net.UDPAddr)))  check(a.SetDeadline(time.Now().Add(3 * time.Second)))  return a, b}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference