struct HostPort
pub struct HostPort {
pub host string
pub port string
}Purpose
Host and port network address structure.
Public fields
hoststring- host
portstring- port
#netIP/port parsing, TCP/UDP IPv4/IPv6 across Linux, macOS, and Windows, and Unix-domain sockets on Linux/macOS.
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
15_udp_echo/main.xu@main
#net
// UDP echo(Go net.ListenPacket / ListenUDP)。
// printf ping | nc -u 127.0.0.1 19001
// TCP 见 14tcp_echo。
fn main() int {
$c := net.listen_packet("udp", "127.0.0.1:19001") >>> (err) {
println(err.msg())
<< 1
}
println("udp://127.0.0.1:19001 echo")
$running := true
while running {
pkt := c.read_from(4096) >>> (err) {
->
}
data := pkt.data + ""
c.write_to(&data, &pkt.addr) >>> (err) {
->
}
}
<< 0
}14_tcp_echo/main.xu@main
#net
// TCP echo(Go net.Listen + Accept + goroutine)。
// printf ping | nc 127.0.0.1 19000
// UDP 见 15udp_echo。
fn handle(c net.Conn) {
$conn := c
buf := conn.read(4096) >>> (err) {
conn.close() >>> (e2) { << }
<<
}
if buf.len == 0 {
conn.close() >>> (err) { << }
<<
}
conn.write(&buf) >>> (err) { << }
conn.close() >>> (err) { << }
}
fn main() int {
ln := net.listen_on("tcp", "127.0.0.1:19000") >>> (err) {
println(err.msg())
<< 1
}
println("tcp://127.0.0.1:19000 echo")
$running := true
while running {
c := ln.accept() >>> (err) {
->
}
xu handle(c)
}
<< 0
}11_http_server/main.xu@main
#net
#net/http
// 自己 listen / accept / 按路径处理(Go HandleFunc 那种)。
// GET / HTML
// GET /hello text
// GET /health ok
// POST /echo 回显 body
// 静态目录见 12file_server;Handler 上下文见 13http_context。
fn path_only(p string) string {
$i := 0
while i < p.len {
if (p[i] & 255) == 63 {
<< p[0:i]
}
i = i + 1
}
<< p + ""
}
fn write_http(fd int, status int, ctype string, body string) int {
$reason := "OK"
if status == 404 {
reason = "Not Found"
}
if status == 400 {
reason = "Bad Request"
}
if status == 405 {
reason = "Method Not Allowed"
}
$out := "HTTP/1.1 " + str(status) + " " + reason + "\r\n"
out = out + "Content-Type: " + ctype + "\r\n"
out = out + "Content-Length: " + str(body.len) + "\r\n"
out = out + "Connection: close\r\n"
out = out + "\r\n"
out = out + body
<< net.conn_write(fd, &out) >>> (err) { >> err }
}
fn index_page() string {
<< "<!doctype html><html><head><meta charset=\"utf-8\"><title>Xu HTTP</title>" +
"<style>body{font-family:ui-sans-serif,system-ui,sans-serif;max-width:40rem;" +
"margin:3rem auto;padding:0 1.5rem;background:#0f1419;color:#e7ecf1}" +
"h1{font-weight:650;letter-spacing:-.03em}a{color:#7dd3fc}" +
"code{background:#1b2430;padding:.15rem .4rem;border-radius:4px}" +
"ul{line-height:1.8}</style></head><body>" +
"<h1>Xu HTTP</h1>" +
"<p>自己 <code>listen</code> / <code>accept</code> / 按 path 分支。</p>" +
"<ul>" +
"<li><a href=\"/hello\">/hello</a></li>" +
"<li><a href=\"/health\">/health</a></li>" +
"<li>POST <code>/echo</code></li>" +
"</ul></body></html>"
}
fn handle(fd int) {
$req := http.new_request()
req.read(fd) >>> (err) {
msg := err.msg()
write_http(fd, 400, "text/plain; charset=utf-8", msg) >>> (e2) { << }
net.conn_close(fd) >>> (e3) { << }
<<
}
method := req.method()
raw_path := req.path()
path := path_only(raw_path)
if path == "/" || path == "/index.html" {
page := index_page()
write_http(fd, 200, "text/html; charset=utf-8", page) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
if path == "/hello" {
body := "hello, xu\n"
write_http(fd, 200, "text/plain; charset=utf-8", body) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
if path == "/health" {
body := "ok\n"
write_http(fd, 200, "text/plain; charset=utf-8", body) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
if path == "/echo" {
if method != "POST" && method != "PUT" {
body := "method not allowed\n"
write_http(fd, 405, "text/plain; charset=utf-8", body) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
echo := req.body()
write_http(fd, 200, "text/plain; charset=utf-8", echo) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
<<
}
miss := "not found: " + path + "\n"
write_http(fd, 404, "text/plain; charset=utf-8", miss) >>> (err) { << }
net.conn_close(fd) >>> (err) { << }
}
fn main() int {
$s := http.new_server()
s.listen("127.0.0.1:18080") >>> (err) {
println(err.msg())
<< 1
}
println("http://127.0.0.1:18080")
$running := true
while running {
fd := s.accept() >>> (err) {
->
}
// The deployable example uses the portable, deterministic path on all
// hosts. The stackful `xu` scheduler has dedicated stress fixtures,
// but it is not yet a production request-execution primitive; keeping
// request handling sequential also gives local and server builds the
// same lifecycle contract. Put TLS, limits and concurrency in the
// reverse proxy until that runtime gate is closed.
handle(fd)
}
<< 0
}struct HostPortpub struct HostPort {
pub host string
pub port string
}Host and port network address structure.
host stringport stringnew_host_portpub fn new_host_port() HostPortConstruct host port.
No arguments are required.
HostPort — a value of this type
(HostPort) hostpub fn (hp HostPort) host() stringOperation on HostPort: host.
hp HostPortHostPort instance used by this call (value receiver).string — result string
(HostPort) portpub fn (hp HostPort) port() stringOperation on HostPort: port.
hp HostPortHostPort instance used by this call (value receiver).string — result string
ipv4pub fn ipv4(a int, b int, c int, d int) stringBuild an IPv4 address from four 0..255 octets (a 4-byte string).
a intb intc intd intstring — result string
parse_ippub fn parse_ip(s &string) stringParse IPv4 (dotted decimal) or IPv6 (colon hex) string into raw IP bytes; empty if invalid.
s &stringraw IP byte string (4 bytes for IPv4, 16 bytes for IPv6)
ip_stringpub fn ip_string(ip &string) stringIP string.
ip &stringstring — result string
ip_is_unspecifiedpub fn ip_is_unspecified(ip &string) boolWhether ip is unspecified.
ip &stringbool — true or false
ip_is_loopbackpub fn ip_is_loopback(ip &string) boolWhether ip is loopback.
ip &stringbool — true or false
ip_is_privatepub fn ip_is_private(ip &string) boolWhether ip is private.
ip &stringbool — true or false
join_host_portpub fn join_host_port(host &string, port &string) stringCombine host and port into a network address string (host:port or [ipv6]:port).
host &stringport &stringcanonical host:port string
($&HostPort) splitpub fn (hp $&HostPort) split(hostport string) intOperation on $&HostPort: split on the separator. Throws on failure; handle the error or propagate it to the caller.
hp $&HostPortHostPort instance used by this call (exclusive borrow).hostport stringint — a value of this type
struct Connpub struct Conn {
pub network string
pub laddr string
pub raddr string
pub read_deadline_ns int
pub write_deadline_ns int
}General bidirectional streaming network connection.
network stringladdr stringraddr stringread_deadline_ns intwrite_deadline_ns intstruct Listenerpub struct Listener {
pub network string
pub addr string
}Network socket server listener for incoming connections.
network stringaddr stringdialpub fn dial(addr string) intDial a connection.
addr stringint — a value of this type
dial_nspub fn dial_ns(addr string, ns int) intdial_ns is the fd-level connect with an optional connect timeout. ns <= 0 means no extra timeout (same as dial). Throws on failure; handle the error or propagate it to the caller.
addr stringns intint — a value of this type
listenpub fn listen(addr string) intListen for connections. Throws on failure; handle the error or propagate it to the caller.
addr stringint — a value of this type
acceptpub fn accept(fd int) intAccept one connection. Throws on failure; handle the error or propagate it to the caller.
fd intint — a value of this type
conn_closepub fn conn_close(fd int) intConnection close. Throws on failure; handle the error or propagate it to the caller.
fd intint — a value of this type
conn_writepub fn conn_write(fd int, data &string) intConnection write. Throws on failure; handle the error or propagate it to the caller.
fd intdata &stringint — a value of this type
conn_readpub fn conn_read(fd int, max int) stringConnection read. Throws on failure; handle the error or propagate it to the caller.
fd intmax intstring — result string
($&Conn) set_deadlinepub fn (c $&Conn) set_deadline(t_ns int) intConn.SetDeadline / SetReadDeadline / SetWriteDeadline. t_ns is absolute now_ns; 0 clears read and/or write timeouts independently.
c $&ConnConn instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&Conn) set_read_deadlinepub fn (c $&Conn) set_read_deadline(t_ns int) intOperation on $&Conn: set read deadline.
c $&ConnConn instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&Conn) set_write_deadlinepub fn (c $&Conn) set_write_deadline(t_ns int) intOperation on $&Conn: set write deadline.
c $&ConnConn instance used by this call (exclusive borrow).t_ns intint — a value of this type
(&Conn) readpub fn (c &Conn) read(max int) stringOperation on &Conn: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).max intstring — result string
(&Conn) writepub fn (c &Conn) write(data &string) intOperation on &Conn: write data and return the byte count. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).data &stringint — bytes written
($&Conn) closepub fn (c $&Conn) close() intOperation on $&Conn: close the resource. Throws on failure; handle the error or propagate it to the caller.
c $&ConnConn instance used by this call (exclusive borrow).int — a value of this type
($&Conn) close_readpub fn (c $&Conn) close_read() intOperation on $&Conn: close read. Throws on failure; handle the error or propagate it to the caller.
c $&ConnConn instance used by this call (exclusive borrow).int — a value of this type
($&Conn) close_writepub fn (c $&Conn) close_write() intOperation on $&Conn: close write. Throws on failure; handle the error or propagate it to the caller.
c $&ConnConn instance used by this call (exclusive borrow).int — a value of this type
($&Conn) shutdownpub fn (c $&Conn) shutdown(how int) intExecute shutdown on $&Conn. Throws on failure; handle the error or propagate it to the caller.
c $&ConnConn instance used by this call (exclusive borrow).how intint — a value of this type
(&Conn) set_nodelaypub fn (c &Conn) set_nodelay(nodelay bool) intOperation on &Conn: set nodelay. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).nodelay boolint — a value of this type
(&Conn) set_keepalivepub fn (c &Conn) set_keepalive(enable bool) intOperation on &Conn: set keepalive. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).enable boolint — a value of this type
(&Conn) set_read_bufferpub fn (c &Conn) set_read_buffer(bytes int) intOperation on &Conn: set read buffer. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).bytes intint — a value of this type
(&Conn) set_write_bufferpub fn (c &Conn) set_write_buffer(bytes int) intOperation on &Conn: set write buffer. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).bytes intint — a value of this type
(Conn) local_addrpub fn (c Conn) local_addr() stringOperation on Conn: local address.
c ConnConn instance used by this call (value receiver).string — result string
(Conn) remote_addrpub fn (c Conn) remote_addr() stringOperation on Conn: remote address.
c ConnConn instance used by this call (value receiver).string — result string
(Listener) acceptpub fn (l Listener) accept() ConnOperation on Listener: accept one connection. Throws on failure; handle the error or propagate it to the caller.
l ListenerListener instance used by this call (value receiver).Conn — a value of this type
($&Listener) closepub fn (l $&Listener) close() intOperation on $&Listener: close the resource. Throws on failure; handle the error or propagate it to the caller.
l $&ListenerListener instance used by this call (exclusive borrow).int — a value of this type
(Listener) addrpub fn (l Listener) addr() stringOperation on Listener: address.
l ListenerListener instance used by this call (value receiver).string — result string
file_connpub fn file_conn(fd int) ConnFile connection. Throws on failure; handle the error or propagate it to the caller.
fd intConn — a value of this type
file_listenerpub fn file_listener(fd int) ListenerFile listener. Throws on failure; handle the error or propagate it to the caller.
fd intListener — a value of this type
closed_connpub fn closed_conn() ConnClosed connection.
No arguments are required.
Conn — a value of this type
(&Conn) is_closedpub fn (c &Conn) is_closed() boolOperation on &Conn: whether it is closed.
c &ConnConn instance used by this call (borrow).bool — true or false
(&Conn) descriptorpub fn (c &Conn) descriptor() intBorrowed raw descriptor; platform adapters must not create a second owner. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).int — a value of this type
struct TCPAddrpub struct TCPAddr {
pub ip string
pub port int
pub zone string
}TCP network endpoint address structure.
ip stringport intzone stringstruct UDPAddrpub struct UDPAddr {
pub ip string
pub port int
pub zone string
}UDP network endpoint address structure.
ip stringport intzone stringstruct IPAddrpub struct IPAddr {
pub ip string
pub zone string
}IP network endpoint address structure.
ip stringzone stringstruct UnixAddrpub struct UnixAddr {
pub name string
pub network string
}Unix network endpoint address structure.
name stringnetwork stringnew_tcp_addrpub fn new_tcp_addr() TCPAddrConstruct tcp addr.
No arguments are required.
TCPAddr — a value of this type
new_udp_addrpub fn new_udp_addr() UDPAddrConstruct udp addr.
No arguments are required.
UDPAddr — a value of this type
new_ip_addrpub fn new_ip_addr() IPAddrConstruct ip addr.
No arguments are required.
IPAddr — a value of this type
new_unix_addrpub fn new_unix_addr() UnixAddrConstruct unix addr.
No arguments are required.
UnixAddr — a value of this type
(TCPAddr) networkpub fn (a TCPAddr) network() stringReturn the network protocol family name (e.g. "tcp", "udp").
a TCPAddrTCPAddr instance used by this call (value receiver).string — result string
(UDPAddr) networkpub fn (a UDPAddr) network() stringReturn the network protocol family name (e.g. "tcp", "udp").
a UDPAddrUDPAddr instance used by this call (value receiver).string — result string
(IPAddr) networkpub fn (a IPAddr) network() stringReturn the network protocol family name (e.g. "tcp", "udp").
a IPAddrIPAddr instance used by this call (value receiver).string — result string
(UnixAddr) networkpub fn (a UnixAddr) network() stringReturn the network protocol family name (e.g. "tcp", "udp").
a UnixAddrUnixAddr instance used by this call (value receiver).string — result string
(TCPAddr) to_stringpub fn (a TCPAddr) to_string() stringOperation on TCPAddr: convert to string.
a TCPAddrTCPAddr instance used by this call (value receiver).string — result string
(&UDPAddr) clonepub fn (a &UDPAddr) clone() UDPAddrOperation on &UDPAddr: copy into a new independent string.
a &UDPAddrUDPAddr instance used by this call (borrow).UDPAddr — a value of this type
(UDPAddr) to_stringpub fn (a UDPAddr) to_string() stringOperation on UDPAddr: convert to string.
a UDPAddrUDPAddr instance used by this call (value receiver).string — result string
(IPAddr) to_stringpub fn (a IPAddr) to_string() stringOperation on IPAddr: convert to string.
a IPAddrIPAddr instance used by this call (value receiver).string — result string
(UnixAddr) to_stringpub fn (a UnixAddr) to_string() stringOperation on UnixAddr: convert to string.
a UnixAddrUnixAddr instance used by this call (value receiver).string — result string
unpack_sockaddrpub fn unpack_sockaddr(raw string) stringUnpack sockaddr.
raw stringstring — result string
socket_local_addrpub fn socket_local_addr(fd int) stringSocket local address.
fd intstring — result string
socket_remote_addrpub fn socket_remote_addr(fd int) stringSocket remote address.
fd intstring — result string
split_host_portpub fn split_host_port(hostport string) HostPortSplit host:port or [ipv6]:port network address into host and port parts.
hostport stringHostPort structure containing host and port
resolve_tcp_addrpub fn resolve_tcp_addr(network string, address string) TCPAddrResolve TCP address. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringTCPAddr — a value of this type
resolve_udp_addrpub fn resolve_udp_addr(network string, address string) UDPAddrResolve UDP address. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringUDPAddr — a value of this type
resolve_ip_addrpub fn resolve_ip_addr(network string, address string) IPAddrResolve IP address. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringIPAddr — a value of this type
resolve_unix_addrpub fn resolve_unix_addr(network string, address string) UnixAddrResolve unix address. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringUnixAddr — a value of this type
struct Dialerpub struct Dialer {
pub timeout_ns int
pub dual_stack bool
pub local_ip string
pub local_port int
}Network connection dialer with custom options.
timeout_ns intdual_stack boollocal_ip stringlocal_port intstruct ListenConfigpub struct ListenConfig {
pub reuse bool
}Network listener configuration with custom options.
reuse boolnew_dialerpub fn new_dialer() DialerConstruct dialer.
No arguments are required.
Dialer — a value of this type
new_listen_configpub fn new_listen_config() ListenConfigConstruct listen config.
No arguments are required.
ListenConfig — a value of this type
dial_tcppub fn dial_tcp(network string, laddr TCPAddr, raddr TCPAddr) ConnDial TCP. Throws on failure; handle the error or propagate it to the caller.
network stringladdr TCPAddrraddr TCPAddrConn — a value of this type
listen_tcp_configpub fn listen_tcp_config(network string, laddr TCPAddr, reuse bool) ListenerListen TCP config. Throws on failure; handle the error or propagate it to the caller.
network stringladdr TCPAddrreuse boolListener — a value of this type
listen_tcppub fn listen_tcp(network string, laddr TCPAddr) ListenerListen TCP.
network stringladdr TCPAddrListener — a value of this type
dial_topub fn dial_to(network string, address string) ConnDial to. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringConn — a value of this type
listen_onpub fn listen_on(network string, address string) ListenerListen on. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringListener — a value of this type
dial_contextpub fn dial_context(ctx &context.Context, network string, address string) Conndial_context is net.DialContext.
ctx &context.Contextnetwork stringaddress stringConn — a value of this type
(Dialer) dialpub fn (d Dialer) dial(network string, address string) ConnOperation on Dialer: dial a connection. Throws on failure; handle the error or propagate it to the caller.
d DialerDialer instance used by this call (value receiver).network stringaddress stringConn — a value of this type
(Dialer) dial_contextpub fn (d Dialer) dial_context(ctx &context.Context, network string, address string) ConnOperation on Dialer: dial context. Throws on failure; handle the error or propagate it to the caller.
d DialerDialer instance used by this call (value receiver).ctx &context.Contextnetwork stringaddress stringConn — a value of this type
dial_timeoutpub fn dial_timeout(network string, address string, timeout_ns int) ConnDial timeout. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringtimeout_ns intConn — a value of this type
(ListenConfig) listenpub fn (c ListenConfig) listen(ctx &context.Context, network string, address string) Listenerlisten is ListenConfig.Listen(ctx, network, address). Throws on failure; handle the error or propagate it to the caller.
c ListenConfigListenConfig instance used by this call (value receiver).ctx &context.Contextnetwork stringaddress stringListener — a value of this type
(ListenConfig) listen_packetpub fn (c ListenConfig) listen_packet(ctx &context.Context, network string, address string) PacketConnOperation on ListenConfig: listen packet. Throws on failure; handle the error or propagate it to the caller.
c ListenConfigListenConfig instance used by this call (value receiver).ctx &context.Contextnetwork stringaddress stringPacketConn — a value of this type
listen_packetpub fn listen_packet(network string, address string) PacketConnListen packet. Throws on failure; handle the error or propagate it to the caller.
network stringaddress stringPacketConn — a value of this type
struct Interfacepub struct Interface {
pub index int
pub mtu int
pub name string
pub hardware_addr string
pub flags int
}System network interface information structure.
index intmtu intname stringhardware_addr stringflags intnew_interfacepub fn new_interface() InterfaceConstruct interface.
No arguments are required.
Interface — a value of this type
flags_stringpub fn flags_string(f int) stringFlags string.
f intstring — result string
(Interface) addrspub fn (ifi Interface) addrs() []stringList all network addresses assigned to this interface. Throws on failure; handle the error or propagate it to the caller.
ifi InterfaceInterface instance used by this call (value receiver).[]string — slice result
interfacespub fn interfaces() []InterfaceInterfaces. Throws on failure; handle the error or propagate it to the caller.
No arguments are required.
[]Interface — slice result
interface_addrspub fn interface_addrs() []stringInterface addrs. Throws on failure; handle the error or propagate it to the caller.
No arguments are required.
[]string — slice result
interface_by_namepub fn interface_by_name(name string) InterfaceInterface by name. Throws on failure; handle the error or propagate it to the caller.
name stringInterface — a value of this type
interface_by_indexpub fn interface_by_index(index int) InterfaceInterface by index. Throws on failure; handle the error or propagate it to the caller.
index intInterface — a value of this type
struct IPNetpub struct IPNet {
pub ip string
pub mask string
}IP network segment structure with prefix mask.
ip stringmask stringstruct CIDRpub struct CIDR {
pub ip string
pub ipnet IPNet
}Structure encapsulating ip, ipnet.
ip stringipnet IPNetipv4_zeropub fn ipv4_zero() stringIPv4 zero.
No arguments are required.
string — result string
ipv4_bcastpub fn ipv4_bcast() stringIPv4 limited broadcast address (255.255.255.255).
No arguments are required.
string — result string
ipv4_allsyspub fn ipv4_allsys() stringIPv4 all-systems multicast address (224.0.0.1).
No arguments are required.
string — result string
ipv4_allrouterpub fn ipv4_allrouter() stringIPv4 all-routers multicast address (224.0.0.2).
No arguments are required.
string — result string
ipv6_zeropub fn ipv6_zero() stringIPv6 zero.
No arguments are required.
string — result string
ipv6_unspecifiedpub fn ipv6_unspecified() stringIPv6 unspecified.
No arguments are required.
string — result string
ipv6_loopbackpub fn ipv6_loopback() stringIPv6 loopback.
No arguments are required.
string — result string
ipv6_interface_local_all_nodespub fn ipv6_interface_local_all_nodes() stringIPv6 interface local all nodes.
No arguments are required.
string — result string
ipv6_link_local_all_nodespub fn ipv6_link_local_all_nodes() stringIPv6 link-local all-nodes multicast address (ff02::1).
No arguments are required.
string — result string
ipv6_link_local_all_routerspub fn ipv6_link_local_all_routers() stringIPv6 link-local all-routers multicast address (ff02::2).
No arguments are required.
string — result string
ipv4_maskpub fn ipv4_mask(a int, b int, c int, d int) stringConstruct 4-byte IPv4 subnet mask.
a intb intc intd intstring — result string
cidr_maskpub fn cidr_mask(ones int, bits int) stringConstruct CIDR IP subnet mask from prefix bit count.
ones intbits intstring — result string
ip_to4pub fn ip_to4(ip &string) stringReturn 4-byte representation if IPv4, or empty string.
ip &stringstring — result string
ip_to16pub fn ip_to16(ip &string) stringReturn standard 16-byte representation of the IP address.
ip &stringstring — result string
ip_equalpub fn ip_equal(a &string, b &string) boolIP equal.
a &stringb &stringbool — true or false
ip_maskpub fn ip_mask(ip &string, mask &string) stringPerform bitwise AND of the IP with subnet mask.
ip &stringmask &stringstring — result string
ip_default_maskpub fn ip_default_mask(ip &string) stringReturn default subnet mask for IPv4 address class.
ip &stringstring — result string
ip_mask_sizepub fn ip_mask_size(mask &string) intIP mask size.
mask &stringint — a value of this type
ip_mask_bitspub fn ip_mask_bits(mask &string) intReturn leading ones and total bits of the mask.
mask &stringint — a value of this type
ip_mask_stringpub fn ip_mask_string(mask &string) stringIP mask string.
mask &stringstring — result string
(IPNet) networkpub fn (n IPNet) network() stringReturn the network protocol family name (e.g. "tcp", "udp").
n IPNetIPNet instance used by this call (value receiver).string — result string
(IPNet) containspub fn (n IPNet) contains(ip &string) boolOperation on IPNet: report whether the input contains the given piece.
n IPNetIPNet instance used by this call (value receiver).ip &stringbool — true or false
(IPNet) to_stringpub fn (n IPNet) to_string() stringOperation on IPNet: convert to string.
n IPNetIPNet instance used by this call (value receiver).string — result string
parse_cidrpub fn parse_cidr(s &string) CIDRParse as cidr. Throws on failure; handle the error or propagate it to the caller.
s &stringCIDR — a value of this type
ip_is_multicastpub fn ip_is_multicast(ip &string) boolWhether ip is multicast.
ip &stringbool — true or false
ip_is_interface_local_multicastpub fn ip_is_interface_local_multicast(ip &string) boolWhether ip is interface_local_multicast.
ip &stringbool — true or false
ip_is_link_local_multicastpub fn ip_is_link_local_multicast(ip &string) boolWhether ip is link_local_multicast.
ip &stringbool — true or false
ip_is_link_local_unicastpub fn ip_is_link_local_unicast(ip &string) boolWhether ip is link_local_unicast.
ip &stringbool — true or false
ip_is_global_unicastpub fn ip_is_global_unicast(ip &string) boolWhether ip is global_unicast.
ip &stringbool — true or false
dial_ippub fn dial_ip(network string, laddr IPAddr, raddr IPAddr) ConnDial IP. Throws on failure; handle the error or propagate it to the caller.
network stringladdr IPAddrraddr IPAddrConn — a value of this type
listen_ippub fn listen_ip(network string, laddr IPAddr) PacketConnListen IP. Throws on failure; handle the error or propagate it to the caller.
network stringladdr IPAddrPacketConn — a value of this type
struct MXpub struct MX {
pub host string
pub pref int
}Structure encapsulating host, pref.
host stringpref intstruct NSpub struct NS {
pub host string
}Structure encapsulating host.
host stringstruct SRVpub struct SRV {
pub target string
pub port int
pub priority int
pub weight int
}Structure encapsulating target, port, priority, weight.
target stringport intpriority intweight intlookup_hostpub fn lookup_host(host string) []stringLookup host. Throws on failure; handle the error or propagate it to the caller.
host string[]string — slice result
lookup_ippub fn lookup_ip(host string) []stringLookup IP. Throws on failure; handle the error or propagate it to the caller.
host string[]string — slice result
lookup_cnamepub fn lookup_cname(host string) stringLookup cname. Throws on failure; handle the error or propagate it to the caller.
host stringstring — result string
lookup_mxpub fn lookup_mx(name string) []MXLookup mx.
name string[]MX — slice result
lookup_nspub fn lookup_ns(name string) []NSLookup ns.
name string[]NS — slice result
lookup_txtpub fn lookup_txt(name string) []stringLookup txt.
name string[]string — slice result
lookup_srvpub fn lookup_srv(service string, proto string, name string) []SRVLookup srv. Throws on failure; handle the error or propagate it to the caller.
service stringproto stringname string[]SRV — slice result
lookup_addrpub fn lookup_addr(addr string) []stringLookup address. Throws on failure; handle the error or propagate it to the caller.
addr string[]string — slice result
hardware_addr_stringpub fn hardware_addr_string(a &string) stringHardware address string.
a &stringstring — result string
parse_macpub fn parse_mac(s &string) stringParse as mac. Throws on failure; handle the error or propagate it to the caller.
s &stringstring — result string
listen_multicast_udppub fn listen_multicast_udp(network string, gaddr UDPAddr) PacketConnlisten_multicast_udp binds 0.0.0.0:gaddr.port with address/port reuse, so it can share the port with a system responder (e.g. mDNS on 5353), and joins gaddr on the default interface. Use join_group for more interfaces. Throws on failure; handle the error or propagate it to the caller.
network stringgaddr UDPAddrPacketConn — a value of this type
(&PacketConn) join_grouppub fn (c &PacketConn) join_group(group string, iface_ip string) intOperation on &PacketConn: join group. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).group stringiface_ip stringint — a value of this type
(&PacketConn) leave_grouppub fn (c &PacketConn) leave_group(group string, iface_ip string) intOperation on &PacketConn: leave group. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).group stringiface_ip stringint — a value of this type
(&PacketConn) set_multicast_interfacepub fn (c &PacketConn) set_multicast_interface(iface_ip string) intOutgoing multicast interface by IPv4 address ("" = system default). Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).iface_ip stringint — a value of this type
(&PacketConn) set_multicast_ttlpub fn (c &PacketConn) set_multicast_ttl(ttl int) intOperation on &PacketConn: set multicast ttl. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).ttl intint — a value of this type
(&PacketConn) set_multicast_loopbackpub fn (c &PacketConn) set_multicast_loopback(on bool) intOperation on &PacketConn: set multicast loopback. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).on boolint — a value of this type
struct PipeConnpub struct PipeConn {
pub w chan string
pub r chan string
pub closed int
}Structure encapsulating w, r, closed.
w chan stringr chan stringclosed intstruct PipePairpub struct PipePair {
pub c1 PipeConn
pub c2 PipeConn
}Structure encapsulating c1, c2.
c1 PipeConnc2 PipeConnpipepub fn pipe() PipePairCreate a pair of connected synchronous in-memory pipe endpoints.
No arguments are required.
PipePair — a value of this type
(PipeConn) readpub fn (c PipeConn) read() stringOperation on PipeConn: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
c PipeConnPipeConn instance used by this call (value receiver).string — result string
(PipeConn) writepub fn (c PipeConn) write(data string) intOperation on PipeConn: write data and return the byte count. Throws on failure; handle the error or propagate it to the caller.
c PipeConnPipeConn instance used by this call (value receiver).data stringint — bytes written
($&PipeConn) closepub fn (c $&PipeConn) close() intOperation on $&PipeConn: close the resource.
c $&PipeConnPipeConn instance used by this call (exclusive borrow).int — a value of this type
(PipeConn) local_addrpub fn (c PipeConn) local_addr() stringOperation on PipeConn: local address.
c PipeConnPipeConn instance used by this call (value receiver).string — result string
(PipeConn) remote_addrpub fn (c PipeConn) remote_addr() stringOperation on PipeConn: remote address.
c PipeConnPipeConn instance used by this call (value receiver).string — result string
(PipeConn) networkpub fn (c PipeConn) network() stringReturn the network protocol family name (e.g. "tcp", "udp").
c PipeConnPipeConn instance used by this call (value receiver).string — result string
lookup_portpub fn lookup_port(network string, service string) intLookup port. Throws on failure; handle the error or propagate it to the caller.
network stringservice stringint — a value of this type
lookup_protocolpub fn lookup_protocol(name string) intLookup protocol. Throws on failure; handle the error or propagate it to the caller.
name stringint — a value of this type
(&Conn) read_resultpub fn (c &Conn) read_result(n int) io.ReadResultOperation on &Conn: read result.
c &ConnConn instance used by this call (borrow).n intio.ReadResult — a value of this type
(&Conn) write_resultpub fn (c &Conn) write_result(data string) io.WriteResultOperation on &Conn: write result.
c &ConnConn instance used by this call (borrow).data stringio.WriteResult — a value of this type
const IPPROTO_TCPpub const IPPROTO_TCP = 6Constant IPPROTO_TCP, defining protocol or algorithm parameters.
const TCP_NODELAYpub const TCP_NODELAY = 1Constant TCP_NODELAY, defining protocol or algorithm parameters.
(&Conn) read_npub fn (c &Conn) read_n(n int) stringio stream adapters preserve the existing per-connection deadline behavior. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).n intstring — result string
(&Conn) write_strpub fn (c &Conn) write_str(data string) intOperation on &Conn: write a string. Throws on failure; handle the error or propagate it to the caller.
c &ConnConn instance used by this call (borrow).data stringint — a value of this type
struct PacketConnpub struct PacketConn {
pub network string
pub laddr string
pub raddr string
}Structure encapsulating network, laddr, raddr.
network stringladdr stringraddr stringstruct UDPPacketpub struct UDPPacket {
pub addr UDPAddr
pub data string
}Structure encapsulating addr, data.
addr UDPAddrdata string(PacketConn) local_addrpub fn (c PacketConn) local_addr() stringOperation on PacketConn: local address.
c PacketConnPacketConn instance used by this call (value receiver).string — result string
(PacketConn) remote_addrpub fn (c PacketConn) remote_addr() stringOperation on PacketConn: remote address.
c PacketConnPacketConn instance used by this call (value receiver).string — result string
($&PacketConn) set_deadlinepub fn (c $&PacketConn) set_deadline(t_ns int) intOperation on $&PacketConn: set deadline.
c $&PacketConnPacketConn instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&PacketConn) set_read_deadlinepub fn (c $&PacketConn) set_read_deadline(t_ns int) intOperation on $&PacketConn: set read deadline.
c $&PacketConnPacketConn instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&PacketConn) set_write_deadlinepub fn (c $&PacketConn) set_write_deadline(t_ns int) intOperation on $&PacketConn: set write deadline.
c $&PacketConnPacketConn instance used by this call (exclusive borrow).t_ns intint — a value of this type
(&PacketConn) set_broadcastpub fn (c &PacketConn) set_broadcast(on bool) intOperation on &PacketConn: set broadcast. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).on boolint — a value of this type
(&PacketConn) set_read_bufferpub fn (c &PacketConn) set_read_buffer(bytes int) intOperation on &PacketConn: set read buffer. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).bytes intint — a value of this type
(&PacketConn) set_write_bufferpub fn (c &PacketConn) set_write_buffer(bytes int) intOperation on &PacketConn: set write buffer. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).bytes intint — a value of this type
(&PacketConn) readpub fn (c &PacketConn) read(max int) stringConnected packet I/O uses socket recv/send so the Windows path reaches Winsock rather than the file-handle WriteFile/ReadFile helpers. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).max intstring — result string
(&PacketConn) writepub fn (c &PacketConn) write(data &string) intOperation on &PacketConn: write data and return the byte count. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).data &stringint — bytes written
(&PacketConn) read_frompub fn (c &PacketConn) read_from(max int) UDPPacketOperation on &PacketConn: read from. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).max intUDPPacket — a value of this type
(&PacketConn) write_topub fn (c &PacketConn) write_to(data &string, addr &UDPAddr) intOperation on &PacketConn: write to. Throws on failure; handle the error or propagate it to the caller.
c &PacketConnPacketConn instance used by this call (borrow).data &stringaddr &UDPAddrint — a value of this type
($&PacketConn) closepub fn (c $&PacketConn) close() intOperation on $&PacketConn: close the resource. Throws on failure; handle the error or propagate it to the caller.
c $&PacketConnPacketConn instance used by this call (exclusive borrow).int — a value of this type
dial_udppub fn dial_udp(network string, laddr UDPAddr, raddr UDPAddr) PacketConnDial UDP. Throws on failure; handle the error or propagate it to the caller.
network stringladdr UDPAddrraddr UDPAddrPacketConn — a value of this type
listen_udp_configpub fn listen_udp_config(network string, laddr UDPAddr, reuse bool) PacketConnListen UDP config. Throws on failure; handle the error or propagate it to the caller.
network stringladdr UDPAddrreuse boolPacketConn — a value of this type
listen_udppub fn listen_udp(network string, laddr UDPAddr) PacketConnListen UDP.
network stringladdr UDPAddrPacketConn — a value of this type
dial_unixpub fn dial_unix(network string, laddr UnixAddr, raddr UnixAddr) ConnDial unix. Throws on failure; handle the error or propagate it to the caller.
network stringladdr UnixAddrraddr UnixAddrConn — a value of this type
listen_unixpub fn listen_unix(network string, laddr UnixAddr) ListenerListen unix. Throws on failure; handle the error or propagate it to the caller.
network stringladdr UnixAddrListener — a value of this type