struct HostPort
pub struct HostPort {
pub host string
pub port string
}用途
网络终端的主机与端口地址结构(包含 host 字符串与 port 整数)。
公开字段
hoststring- 主机
portstring- 端口
#netIP/端口解析、跨平台 TCP/UDP IPv4/IPv6(Linux/macOS/Windows)与 Unix 域套接字(仅 Linux/macOS)。
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
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 字符串与 port 整数)。
host stringport stringnew_host_portpub fn new_host_port() HostPort构造 host port。
无需传入参数。
HostPort — 该类型的值
(HostPort) hostpub fn (hp HostPort) host() stringHostPort 上的操作:主机。
hp HostPortHostPort 实例;按值接收。string — 结果字符串
(HostPort) portpub fn (hp HostPort) port() stringHostPort 上的操作:端口。
hp HostPortHostPort 实例;按值接收。string — 结果字符串
ipv4pub fn ipv4(a int, b int, c int, d int) string由四个 0..255 的字节构造 IPv4 地址(长度为 4 的 string)。
a intb intc intd intstring — 结果字符串
parse_ippub fn parse_ip(s &string) string将 IPv4(点分十进制)或 IPv6(十六进制冒号)字符串解析为标准字节 IP;非法地址返回空串。
s &string4 字节(IPv4)或 16 字节(IPv6)的原始 IP 字节串
ip_stringpub fn ip_string(ip &string) string执行 ip string 操作。
ip &stringstring — 结果字符串
ip_is_unspecifiedpub fn ip_is_unspecified(ip &string) bool是否未指定地址。
ip &stringbool — 是否成立
ip_is_loopbackpub fn ip_is_loopback(ip &string) bool是否回环地址。
ip &stringbool — 是否成立
ip_is_privatepub fn ip_is_private(ip &string) bool是否私网地址。
ip &stringbool — 是否成立
join_host_portpub fn join_host_port(host &string, port &string) string将主机名或 IP 与端口号组合为网络终端地址串(如 host:port 或 [ipv6]:port)。
host &stringport &string规范化后的 host:port 地址串
($&HostPort) splitpub fn (hp $&HostPort) split(hostport string) int$&HostPort 上的操作:按分隔符切开。失败时抛出错误,调用方需处理或继续传播。
hp $&HostPortHostPort 实例;以独占借用传入,方法可修改它,调用方仍持有实例。hostport stringint — 该类型的值
struct Connpub struct Conn {
pub network string
pub laddr string
pub raddr string
pub read_deadline_ns int
pub write_deadline_ns int
}通用网络流式双向连接,提供读写、超时控制与文件描述符获取。
network stringladdr stringraddr stringread_deadline_ns intwrite_deadline_ns intstruct Listenerpub struct Listener {
pub network string
pub addr string
}网络套接字服务端监听器,用于等待并接受传入连接。
network stringaddr stringdialpub fn dial(addr string) int发起连接。
addr stringint — 该类型的值
dial_nspub fn dial_ns(addr string, ns int) int拨号纳秒。失败时抛出错误,调用方需处理或继续传播。
addr stringns intint — 该类型的值
listenpub fn listen(addr string) int开始监听。失败时抛出错误,调用方需处理或继续传播。
addr stringint — 该类型的值
acceptpub fn accept(fd int) int接受一个连接。失败时抛出错误,调用方需处理或继续传播。
fd intint — 该类型的值
conn_closepub fn conn_close(fd int) int关闭连接。失败时抛出错误,调用方需处理或继续传播。
fd intint — 该类型的值
conn_writepub fn conn_write(fd int, data &string) int向连接写。失败时抛出错误,调用方需处理或继续传播。
fd intdata &stringint — 该类型的值
conn_readpub fn conn_read(fd int, max int) string从连接读。失败时抛出错误,调用方需处理或继续传播。
fd intmax intstring — 结果字符串
($&Conn) set_deadlinepub fn (c $&Conn) set_deadline(t_ns int) int$&Conn 上的操作:设置 deadline。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&Conn) set_read_deadlinepub fn (c $&Conn) set_read_deadline(t_ns int) int$&Conn 上的操作:设置 read deadline。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&Conn) set_write_deadlinepub fn (c $&Conn) set_write_deadline(t_ns int) int$&Conn 上的操作:设置 write deadline。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
(&Conn) readpub fn (c &Conn) read(max int) string&Conn 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。max intstring — 结果字符串
(&Conn) writepub fn (c &Conn) write(data &string) int&Conn 上的操作:写入数据,返回写入的字节数。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。data &stringint — 写入的字节数
($&Conn) closepub fn (c $&Conn) close() int$&Conn 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&Conn) close_readpub fn (c $&Conn) close_read() int$&Conn 上的操作:读取关闭。失败时抛出错误,调用方需处理或继续传播。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&Conn) close_writepub fn (c $&Conn) close_write() int$&Conn 上的操作:写入关闭。失败时抛出错误,调用方需处理或继续传播。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&Conn) shutdownpub fn (c $&Conn) shutdown(how int) int在 $&Conn 上执行 shutdown 操作。失败时抛出错误,调用方需处理或继续传播。
c $&ConnConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。how intint — 该类型的值
(&Conn) set_nodelaypub fn (c &Conn) set_nodelay(nodelay bool) int&Conn 上的操作:设置 nodelay。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。nodelay boolint — 该类型的值
(&Conn) set_keepalivepub fn (c &Conn) set_keepalive(enable bool) int&Conn 上的操作:设置 keepalive。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。enable boolint — 该类型的值
(&Conn) set_read_bufferpub fn (c &Conn) set_read_buffer(bytes int) int&Conn 上的操作:设置 read buffer。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。bytes intint — 该类型的值
(&Conn) set_write_bufferpub fn (c &Conn) set_write_buffer(bytes int) int&Conn 上的操作:设置 write buffer。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。bytes intint — 该类型的值
(Conn) local_addrpub fn (c Conn) local_addr() stringConn 上的操作:执行 local addr 操作。
c ConnConn 实例;按值接收。string — 结果字符串
(Conn) remote_addrpub fn (c Conn) remote_addr() stringConn 上的操作:执行 remote addr 操作。
c ConnConn 实例;按值接收。string — 结果字符串
(Listener) acceptpub fn (l Listener) accept() ConnListener 上的操作:接受一个连接。失败时抛出错误,调用方需处理或继续传播。
l ListenerListener 实例;按值接收。Conn — 该类型的值
($&Listener) closepub fn (l $&Listener) close() int$&Listener 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
l $&ListenerListener 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
(Listener) addrpub fn (l Listener) addr() stringListener 上的操作:地址。
l ListenerListener 实例;按值接收。string — 结果字符串
file_connpub fn file_conn(fd int) Conn从文件描述符构造连接。失败时抛出错误,调用方需处理或继续传播。
fd intConn — 该类型的值
file_listenerpub fn file_listener(fd int) Listener从文件描述符构造监听器。失败时抛出错误,调用方需处理或继续传播。
fd intListener — 该类型的值
closed_connpub fn closed_conn() Conn执行 closed conn 操作。
无需传入参数。
Conn — 该类型的值
(&Conn) is_closedpub fn (c &Conn) is_closed() bool&Conn 上的操作:判断是否 closed。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Conn) descriptorpub fn (c &Conn) descriptor() int获取底层持有的操作系统原生套接字或文件描述符(fd)。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
struct TCPAddrpub struct TCPAddr {
pub ip string
pub port int
pub zone string
}TCP 网络终端地址结构(包含 IP/路径与端口)。
ip stringport intzone stringstruct UDPAddrpub struct UDPAddr {
pub ip string
pub port int
pub zone string
}UDP 网络终端地址结构(包含 IP/路径与端口)。
ip stringport intzone stringstruct IPAddrpub struct IPAddr {
pub ip string
pub zone string
}IP 网络终端地址结构(包含 IP/路径与端口)。
ip stringzone stringstruct UnixAddrpub struct UnixAddr {
pub name string
pub network string
}Unix 网络终端地址结构(包含 IP/路径与端口)。
name stringnetwork stringnew_tcp_addrpub fn new_tcp_addr() TCPAddr构造 tcp addr。
无需传入参数。
TCPAddr — 该类型的值
new_udp_addrpub fn new_udp_addr() UDPAddr构造 udp addr。
无需传入参数。
UDPAddr — 该类型的值
new_ip_addrpub fn new_ip_addr() IPAddr构造 ip addr。
无需传入参数。
IPAddr — 该类型的值
new_unix_addrpub fn new_unix_addr() UnixAddr构造 unix addr。
无需传入参数。
UnixAddr — 该类型的值
(TCPAddr) networkpub fn (a TCPAddr) network() string返回该网络连接或地址所属的协议族名称(如 "tcp" / "udp" / "unix")。
a TCPAddrTCPAddr 实例;按值接收。string — 结果字符串
(UDPAddr) networkpub fn (a UDPAddr) network() string返回该网络连接或地址所属的协议族名称(如 "tcp" / "udp" / "unix")。
a UDPAddrUDPAddr 实例;按值接收。string — 结果字符串
(IPAddr) networkpub fn (a IPAddr) network() string返回该网络连接或地址所属的协议族名称(如 "tcp" / "udp" / "unix")。
a IPAddrIPAddr 实例;按值接收。string — 结果字符串
(UnixAddr) networkpub fn (a UnixAddr) network() string返回该网络连接或地址所属的协议族名称(如 "tcp" / "udp" / "unix")。
a UnixAddrUnixAddr 实例;按值接收。string — 结果字符串
(TCPAddr) to_stringpub fn (a TCPAddr) to_string() stringTCPAddr 上的操作:转成 string。
a TCPAddrTCPAddr 实例;按值接收。string — 结果字符串
(&UDPAddr) clonepub fn (a &UDPAddr) clone() UDPAddr&UDPAddr 上的操作:拷贝一份独立的 string。
a &UDPAddrUDPAddr 实例;以只读借用传入,调用方仍持有实例。UDPAddr — 该类型的值
(UDPAddr) to_stringpub fn (a UDPAddr) to_string() stringUDPAddr 上的操作:转成 string。
a UDPAddrUDPAddr 实例;按值接收。string — 结果字符串
(IPAddr) to_stringpub fn (a IPAddr) to_string() stringIPAddr 上的操作:转成 string。
a IPAddrIPAddr 实例;按值接收。string — 结果字符串
(UnixAddr) to_stringpub fn (a UnixAddr) to_string() stringUnixAddr 上的操作:转成 string。
a UnixAddrUnixAddr 实例;按值接收。string — 结果字符串
unpack_sockaddrpub fn unpack_sockaddr(raw string) string执行 unpack sockaddr 操作。
raw stringstring — 结果字符串
socket_local_addrpub fn socket_local_addr(fd int) string执行 socket local addr 操作。
fd intstring — 结果字符串
socket_remote_addrpub fn socket_remote_addr(fd int) string执行 socket remote addr 操作。
fd intstring — 结果字符串
split_host_portpub fn split_host_port(hostport string) HostPort将 host:port 或 [ipv6]:port 格式的网络终端地址字符串拆分为主机名与端口部分。
hostport string包含 host 与 port 的 HostPort 结构体
resolve_tcp_addrpub fn resolve_tcp_addr(network string, address string) TCPAddr解析 TCP 地址。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringTCPAddr — 该类型的值
resolve_udp_addrpub fn resolve_udp_addr(network string, address string) UDPAddr解析 UDP 地址。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringUDPAddr — 该类型的值
resolve_ip_addrpub fn resolve_ip_addr(network string, address string) IPAddr解析 IP 地址。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringIPAddr — 该类型的值
resolve_unix_addrpub fn resolve_unix_addr(network string, address string) UnixAddr解析 Unix 地址。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringUnixAddr — 该类型的值
struct Dialerpub struct Dialer {
pub timeout_ns int
pub dual_stack bool
pub local_ip string
pub local_port int
}网络连接拨号器,支持自定义超时、本地地址与连接控制选项。
timeout_ns intdual_stack boollocal_ip stringlocal_port intstruct ListenConfigpub struct ListenConfig {
pub reuse bool
}网络套接字监听配置器,提供可定制的监听参数。
reuse boolnew_dialerpub fn new_dialer() Dialer构造 dialer。
无需传入参数。
Dialer — 该类型的值
new_listen_configpub fn new_listen_config() ListenConfig构造 listen config。
无需传入参数。
ListenConfig — 该类型的值
dial_tcppub fn dial_tcp(network string, laddr TCPAddr, raddr TCPAddr) Conn拨 TCP。失败时抛出错误,调用方需处理或继续传播。
network stringladdr TCPAddrraddr TCPAddrConn — 该类型的值
listen_tcp_configpub fn listen_tcp_config(network string, laddr TCPAddr, reuse bool) Listener执行 listen tcp config 操作。失败时抛出错误,调用方需处理或继续传播。
network stringladdr TCPAddrreuse boolListener — 该类型的值
listen_tcppub fn listen_tcp(network string, laddr TCPAddr) Listener听 TCP。
network stringladdr TCPAddrListener — 该类型的值
dial_topub fn dial_to(network string, address string) Conn拨到地址。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringConn — 该类型的值
listen_onpub fn listen_on(network string, address string) Listener在地址上听。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringListener — 该类型的值
dial_contextpub fn dial_context(ctx &context.Context, network string, address string) Conn带上下文拨号。
ctx &context.Contextnetwork stringaddress stringConn — 该类型的值
(Dialer) dialpub fn (d Dialer) dial(network string, address string) ConnDialer 上的操作:发起连接。失败时抛出错误,调用方需处理或继续传播。
d DialerDialer 实例;按值接收。network stringaddress stringConn — 该类型的值
(Dialer) dial_contextpub fn (d Dialer) dial_context(ctx &context.Context, network string, address string) ConnDialer 上的操作:带上下文拨号。失败时抛出错误,调用方需处理或继续传播。
d DialerDialer 实例;按值接收。ctx &context.Contextnetwork stringaddress stringConn — 该类型的值
dial_timeoutpub fn dial_timeout(network string, address string, timeout_ns int) Conn带超时拨号。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringtimeout_ns intConn — 该类型的值
(ListenConfig) listenpub fn (c ListenConfig) listen(ctx &context.Context, network string, address string) ListenerListenConfig 上的操作:开始监听。失败时抛出错误,调用方需处理或继续传播。
c ListenConfigListenConfig 实例;按值接收。ctx &context.Contextnetwork stringaddress stringListener — 该类型的值
(ListenConfig) listen_packetpub fn (c ListenConfig) listen_packet(ctx &context.Context, network string, address string) PacketConnListenConfig 上的操作:听数据报。失败时抛出错误,调用方需处理或继续传播。
c ListenConfigListenConfig 实例;按值接收。ctx &context.Contextnetwork stringaddress stringPacketConn — 该类型的值
listen_packetpub fn listen_packet(network string, address string) PacketConn听数据报。失败时抛出错误,调用方需处理或继续传播。
network stringaddress stringPacketConn — 该类型的值
struct Interfacepub struct Interface {
pub index int
pub mtu int
pub name string
pub hardware_addr string
pub flags int
}系统网络接口(网卡)信息结构(包含索引、名字与标志位)。
index intmtu intname stringhardware_addr stringflags intnew_interfacepub fn new_interface() Interface构造 interface。
无需传入参数。
Interface — 该类型的值
flags_stringpub fn flags_string(f int) string执行 flags string 操作。
f intstring — 结果字符串
(Interface) addrspub fn (ifi Interface) addrs() []string获取该网络接口上绑定的所有 IP 地址列表。失败时抛出错误,调用方需处理或继续传播。
ifi InterfaceInterface 实例;按值接收。[]string — 切片结果
interfacespub fn interfaces() []Interface网卡列表。失败时抛出错误,调用方需处理或继续传播。
无需传入参数。
[]Interface — 切片结果
interface_addrspub fn interface_addrs() []string网卡地址。失败时抛出错误,调用方需处理或继续传播。
无需传入参数。
[]string — 切片结果
interface_by_namepub fn interface_by_name(name string) Interface按名取网卡。失败时抛出错误,调用方需处理或继续传播。
name stringInterface — 该类型的值
interface_by_indexpub fn interface_by_index(index int) Interface按下标取网卡。失败时抛出错误,调用方需处理或继续传播。
index intInterface — 该类型的值
struct IPNetpub struct IPNet {
pub ip string
pub mask string
}包含 IP 地址及其网络前缀掩码的网段结构。
ip stringmask stringstruct CIDRpub struct CIDR {
pub ip string
pub ipnet IPNet
}承载 ip、ipnet 等属性的结构体。
ip stringipnet IPNetipv4_zeropub fn ipv4_zero() stringIPv4 全零地址。
无需传入参数。
string — 结果字符串
ipv4_bcastpub fn ipv4_bcast() stringIPv4 受限广播地址(255.255.255.255)。
无需传入参数。
string — 结果字符串
ipv4_allsyspub fn ipv4_allsys() stringIPv4 全主机组播地址(224.0.0.1)。
无需传入参数。
string — 结果字符串
ipv4_allrouterpub fn ipv4_allrouter() stringIPv4 全路由器组播地址(224.0.0.2)。
无需传入参数。
string — 结果字符串
ipv6_zeropub fn ipv6_zero() stringIPv6全零。
无需传入参数。
string — 结果字符串
ipv6_unspecifiedpub fn ipv6_unspecified() stringIPv6未指定。
无需传入参数。
string — 结果字符串
ipv6_loopbackpub fn ipv6_loopback() stringIPv6回环。
无需传入参数。
string — 结果字符串
ipv6_interface_local_all_nodespub fn ipv6_interface_local_all_nodes() string执行 ipv6 interface local all nodes 操作。
无需传入参数。
string — 结果字符串
ipv6_link_local_all_nodespub fn ipv6_link_local_all_nodes() stringIPv6 链路本地所有节点组播地址(ff02::1)。
无需传入参数。
string — 结果字符串
ipv6_link_local_all_routerspub fn ipv6_link_local_all_routers() stringIPv6 链路本地所有路由器组播地址(ff02::2)。
无需传入参数。
string — 结果字符串
ipv4_maskpub fn ipv4_mask(a int, b int, c int, d int) string构造 4 字节的 IPv4 子网掩码。
a intb intc intd intstring — 结果字符串
cidr_maskpub fn cidr_mask(ones int, bits int) string根据前缀比特数构造 CIDR 格式的 IP 子网掩码。
ones intbits intstring — 结果字符串
ip_to4pub fn ip_to4(ip &string) string若为 IPv4 地址或 IPv4 映射,返回其 4 字节表示;否则返回空串。
ip &stringstring — 结果字符串
ip_to16pub fn ip_to16(ip &string) string返回该 IP 地址的标准 16 字节形式表示。
ip &stringstring — 结果字符串
ip_equalpub fn ip_equal(a &string, b &string) bool执行 ip equal 操作。
a &stringb &stringbool — 是否成立
ip_maskpub fn ip_mask(ip &string, mask &string) string使用给定的子网掩码与当前 IP 地址执行按位与运算。
ip &stringmask &stringstring — 结果字符串
ip_default_maskpub fn ip_default_mask(ip &string) string返回该 IPv4 地址所属类别的默认子网掩码(A/B/C 类)。
ip &stringstring — 结果字符串
ip_mask_sizepub fn ip_mask_size(mask &string) int执行 ip mask size 操作。
mask &stringint — 该类型的值
ip_mask_bitspub fn ip_mask_bits(mask &string) int返回子网掩码中 1 的位数与总位数 (ones, bits)。
mask &stringint — 该类型的值
ip_mask_stringpub fn ip_mask_string(mask &string) string执行 ip mask string 操作。
mask &stringstring — 结果字符串
(IPNet) networkpub fn (n IPNet) network() string返回该网络连接或地址所属的协议族名称(如 "tcp" / "udp" / "unix")。
n IPNetIPNet 实例;按值接收。string — 结果字符串
(IPNet) containspub fn (n IPNet) contains(ip &string) boolIPNet 上的操作:判断是否包含给定片段。
n IPNetIPNet 实例;按值接收。ip &stringbool — 是否成立
(IPNet) to_stringpub fn (n IPNet) to_string() stringIPNet 上的操作:转成 string。
n IPNetIPNet 实例;按值接收。string — 结果字符串
parse_cidrpub fn parse_cidr(s &string) CIDR解析为 cidr。失败时抛出错误,调用方需处理或继续传播。
s &stringCIDR — 该类型的值
ip_is_multicastpub fn ip_is_multicast(ip &string) bool判断 ip 是否为 multicast。
ip &stringbool — 是否成立
ip_is_interface_local_multicastpub fn ip_is_interface_local_multicast(ip &string) bool判断 ip 是否为 interface_local_multicast。
ip &stringbool — 是否成立
ip_is_link_local_multicastpub fn ip_is_link_local_multicast(ip &string) bool判断 ip 是否为 link_local_multicast。
ip &stringbool — 是否成立
ip_is_link_local_unicastpub fn ip_is_link_local_unicast(ip &string) bool判断 ip 是否为 link_local_unicast。
ip &stringbool — 是否成立
ip_is_global_unicastpub fn ip_is_global_unicast(ip &string) bool判断 ip 是否为 global_unicast。
ip &stringbool — 是否成立
dial_ippub fn dial_ip(network string, laddr IPAddr, raddr IPAddr) Conn执行 dial ip 操作。失败时抛出错误,调用方需处理或继续传播。
network stringladdr IPAddrraddr IPAddrConn — 该类型的值
listen_ippub fn listen_ip(network string, laddr IPAddr) PacketConn执行 listen ip 操作。失败时抛出错误,调用方需处理或继续传播。
network stringladdr IPAddrPacketConn — 该类型的值
struct MXpub struct MX {
pub host string
pub pref int
}承载 host、pref 等属性的结构体。
host stringpref intstruct NSpub struct NS {
pub host string
}承载 host 等属性的结构体。
host stringstruct SRVpub struct SRV {
pub target string
pub port int
pub priority int
pub weight int
}承载 target、port、priority、weight 等属性的结构体。
target stringport intpriority intweight intlookup_hostpub fn lookup_host(host string) []string查找主机。失败时抛出错误,调用方需处理或继续传播。
host string[]string — 切片结果
lookup_ippub fn lookup_ip(host string) []string执行 lookup ip 操作。失败时抛出错误,调用方需处理或继续传播。
host string[]string — 切片结果
lookup_cnamepub fn lookup_cname(host string) string执行 lookup cname 操作。失败时抛出错误,调用方需处理或继续传播。
host stringstring — 结果字符串
lookup_mxpub fn lookup_mx(name string) []MX执行 lookup mx 操作。
name string[]MX — 切片结果
lookup_nspub fn lookup_ns(name string) []NS查找纳秒。
name string[]NS — 切片结果
lookup_txtpub fn lookup_txt(name string) []string执行 lookup txt 操作。
name string[]string — 切片结果
lookup_srvpub fn lookup_srv(service string, proto string, name string) []SRV执行 lookup srv 操作。失败时抛出错误,调用方需处理或继续传播。
service stringproto stringname string[]SRV — 切片结果
lookup_addrpub fn lookup_addr(addr string) []string查找地址。失败时抛出错误,调用方需处理或继续传播。
addr string[]string — 切片结果
hardware_addr_stringpub fn hardware_addr_string(a &string) string执行 hardware addr string 操作。
a &stringstring — 结果字符串
parse_macpub fn parse_mac(s &string) string解析为 mac。失败时抛出错误,调用方需处理或继续传播。
s &stringstring — 结果字符串
listen_multicast_udppub fn listen_multicast_udp(network string, gaddr UDPAddr) PacketConn执行 listen multicast udp 操作。失败时抛出错误,调用方需处理或继续传播。
network stringgaddr UDPAddrPacketConn — 该类型的值
(&PacketConn) join_grouppub fn (c &PacketConn) join_group(group string, iface_ip string) int&PacketConn 上的操作:执行 join group 操作。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。group stringiface_ip stringint — 该类型的值
(&PacketConn) leave_grouppub fn (c &PacketConn) leave_group(group string, iface_ip string) int&PacketConn 上的操作:执行 leave group 操作。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。group stringiface_ip stringint — 该类型的值
(&PacketConn) set_multicast_interfacepub fn (c &PacketConn) set_multicast_interface(iface_ip string) int&PacketConn 上的操作:设置 multicast interface。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。iface_ip stringint — 该类型的值
(&PacketConn) set_multicast_ttlpub fn (c &PacketConn) set_multicast_ttl(ttl int) int&PacketConn 上的操作:设置 multicast ttl。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。ttl intint — 该类型的值
(&PacketConn) set_multicast_loopbackpub fn (c &PacketConn) set_multicast_loopback(on bool) int&PacketConn 上的操作:设置 multicast loopback。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。on boolint — 该类型的值
struct PipeConnpub struct PipeConn {
pub w chan string
pub r chan string
pub closed int
}承载 w、r、closed 等属性的结构体。
w chan stringr chan stringclosed intstruct PipePairpub struct PipePair {
pub c1 PipeConn
pub c2 PipeConn
}承载 c1、c2 等属性的结构体。
c1 PipeConnc2 PipeConnpipepub fn pipe() PipePair创建一对连接在一起的同步内存管道端点。
无需传入参数。
PipePair — 该类型的值
(PipeConn) readpub fn (c PipeConn) read() stringPipeConn 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c PipeConnPipeConn 实例;按值接收。string — 结果字符串
(PipeConn) writepub fn (c PipeConn) write(data string) intPipeConn 上的操作:写入数据,返回写入的字节数。失败时抛出错误,调用方需处理或继续传播。
c PipeConnPipeConn 实例;按值接收。data stringint — 写入的字节数
($&PipeConn) closepub fn (c $&PipeConn) close() int$&PipeConn 上的操作:关闭资源。
c $&PipeConnPipeConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
(PipeConn) local_addrpub fn (c PipeConn) local_addr() stringPipeConn 上的操作:执行 local addr 操作。
c PipeConnPipeConn 实例;按值接收。string — 结果字符串
(PipeConn) remote_addrpub fn (c PipeConn) remote_addr() stringPipeConn 上的操作:执行 remote addr 操作。
c PipeConnPipeConn 实例;按值接收。string — 结果字符串
(PipeConn) networkpub fn (c PipeConn) network() string返回该网络连接或地址所属的协议族名称(如 "tcp" / "udp" / "unix")。
c PipeConnPipeConn 实例;按值接收。string — 结果字符串
lookup_portpub fn lookup_port(network string, service string) int查找端口。失败时抛出错误,调用方需处理或继续传播。
network stringservice stringint — 该类型的值
lookup_protocolpub fn lookup_protocol(name string) int执行 lookup protocol 操作。失败时抛出错误,调用方需处理或继续传播。
name stringint — 该类型的值
(&Conn) read_resultpub fn (c &Conn) read_result(n int) io.ReadResult&Conn 上的操作:读取结果。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。n intio.ReadResult — 该类型的值
(&Conn) write_resultpub fn (c &Conn) write_result(data string) io.WriteResult&Conn 上的操作:写入结果。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。data stringio.WriteResult — 该类型的值
const IPPROTO_TCPpub const IPPROTO_TCP = 6常量 IPPROTO_TCP,定义了相关的协议或算法配置参数。
const TCP_NODELAYpub const TCP_NODELAY = 1常量 TCP_NODELAY,定义了相关的协议或算法配置参数。
(&Conn) read_npub fn (c &Conn) read_n(n int) string&Conn 上的操作:最多读 n 字节;到末尾可返回更短或空串。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。n intstring — 结果字符串
(&Conn) write_strpub fn (c &Conn) write_str(data string) int&Conn 上的操作:写入字符串。失败时抛出错误,调用方需处理或继续传播。
c &ConnConn 实例;以只读借用传入,调用方仍持有实例。data stringint — 该类型的值
struct PacketConnpub struct PacketConn {
pub network string
pub laddr string
pub raddr string
}承载 network、laddr、raddr 等属性的结构体。
network stringladdr stringraddr stringstruct UDPPacketpub struct UDPPacket {
pub addr UDPAddr
pub data string
}承载 addr、data 等属性的结构体。
addr UDPAddrdata string(PacketConn) local_addrpub fn (c PacketConn) local_addr() stringPacketConn 上的操作:执行 local addr 操作。
c PacketConnPacketConn 实例;按值接收。string — 结果字符串
(PacketConn) remote_addrpub fn (c PacketConn) remote_addr() stringPacketConn 上的操作:执行 remote addr 操作。
c PacketConnPacketConn 实例;按值接收。string — 结果字符串
($&PacketConn) set_deadlinepub fn (c $&PacketConn) set_deadline(t_ns int) int$&PacketConn 上的操作:设置 deadline。
c $&PacketConnPacketConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&PacketConn) set_read_deadlinepub fn (c $&PacketConn) set_read_deadline(t_ns int) int$&PacketConn 上的操作:设置 read deadline。
c $&PacketConnPacketConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&PacketConn) set_write_deadlinepub fn (c $&PacketConn) set_write_deadline(t_ns int) int$&PacketConn 上的操作:设置 write deadline。
c $&PacketConnPacketConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
(&PacketConn) set_broadcastpub fn (c &PacketConn) set_broadcast(on bool) int&PacketConn 上的操作:设置 broadcast。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。on boolint — 该类型的值
(&PacketConn) set_read_bufferpub fn (c &PacketConn) set_read_buffer(bytes int) int&PacketConn 上的操作:设置 read buffer。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。bytes intint — 该类型的值
(&PacketConn) set_write_bufferpub fn (c &PacketConn) set_write_buffer(bytes int) int&PacketConn 上的操作:设置 write buffer。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。bytes intint — 该类型的值
(&PacketConn) readpub fn (c &PacketConn) read(max int) string&PacketConn 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。max intstring — 结果字符串
(&PacketConn) writepub fn (c &PacketConn) write(data &string) int&PacketConn 上的操作:写入数据,返回写入的字节数。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。data &stringint — 写入的字节数
(&PacketConn) read_frompub fn (c &PacketConn) read_from(max int) UDPPacket&PacketConn 上的操作:读取从。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。max intUDPPacket — 该类型的值
(&PacketConn) write_topub fn (c &PacketConn) write_to(data &string, addr &UDPAddr) int&PacketConn 上的操作:写入到。失败时抛出错误,调用方需处理或继续传播。
c &PacketConnPacketConn 实例;以只读借用传入,调用方仍持有实例。data &stringaddr &UDPAddrint — 该类型的值
($&PacketConn) closepub fn (c $&PacketConn) close() int$&PacketConn 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&PacketConnPacketConn 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
dial_udppub fn dial_udp(network string, laddr UDPAddr, raddr UDPAddr) PacketConn执行 dial udp 操作。失败时抛出错误,调用方需处理或继续传播。
network stringladdr UDPAddrraddr UDPAddrPacketConn — 该类型的值
listen_udp_configpub fn listen_udp_config(network string, laddr UDPAddr, reuse bool) PacketConn执行 listen udp config 操作。失败时抛出错误,调用方需处理或继续传播。
network stringladdr UDPAddrreuse boolPacketConn — 该类型的值
listen_udppub fn listen_udp(network string, laddr UDPAddr) PacketConn执行 listen udp 操作。
network stringladdr UDPAddrPacketConn — 该类型的值
dial_unixpub fn dial_unix(network string, laddr UnixAddr, raddr UnixAddr) Conn拨号Unix 时间。失败时抛出错误,调用方需处理或继续传播。
network stringladdr UnixAddrraddr UnixAddrConn — 该类型的值
listen_unixpub fn listen_unix(network string, laddr UnixAddr) Listener监听Unix 时间。失败时抛出错误,调用方需处理或继续传播。
network stringladdr UnixAddrListener — 该类型的值