netutils

package
v0.0.0-...-98f1bff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2025 License: Unlicense Imports: 10 Imported by: 0

Documentation

Overview

Package netutils 提供网络相关的工具函数

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractHostPort

func ExtractHostPort(urlStr string) (string, int, error)

ExtractHostPort 从URL中提取主机名和端口号 参数:

  • urlStr: URL字符串

返回:

  • string: 主机名
  • int: 端口号
  • error: 如果解析失败则返回错误信息

func IsPortOpen

func IsPortOpen(host string, port int, timeout time.Duration) (bool, error)

IsPortOpen 检查指定IP和端口是否开放 参数:

  • host: 主机名或IP地址
  • port: 端口号
  • timeout: 连接超时时间

返回:

  • bool: 端口是否开放
  • error: 如果发生错误则返回错误信息

func IsURLReachable

func IsURLReachable(urlStr string, timeout time.Duration) (bool, int, error)

IsURLReachable 检查URL是否可访问 参数:

  • urlStr: 要检查的URL
  • timeout: 请求超时时间

返回:

  • bool: URL是否可访问
  • int: HTTP状态码 (如果请求成功)
  • error: 如果发生错误则返回错误信息

func IsValidDomain

func IsValidDomain(domain string) bool

IsValidDomain 检查字符串是否为有效的域名 返回布尔值表示是否有效

func IsValidIP

func IsValidIP(ipStr string) bool

IsValidIP 检查字符串是否为有效的IPv4或IPv6地址 返回布尔值表示是否有效

func IsValidIPv4

func IsValidIPv4(ipStr string) bool

IsValidIPv4 检查字符串是否为有效的IPv4地址 返回布尔值表示是否有效

func IsValidIPv6

func IsValidIPv6(ipStr string) bool

IsValidIPv6 检查字符串是否为有效的IPv6地址 返回布尔值表示是否有效

Types

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

HTTPClient 是一个简化的HTTP客户端

func NewHTTPClient

func NewHTTPClient(timeout time.Duration) *HTTPClient

NewHTTPClient 创建一个新的HTTP客户端 参数:

  • timeout: 请求超时时间

返回:

  • *HTTPClient: 新创建的HTTP客户端

func (*HTTPClient) Get

func (c *HTTPClient) Get(url string, headers map[string]string) (*HTTPResponse, error)

Get 发送GET请求 参数:

  • url: 请求URL
  • headers: 请求头

返回:

  • *HTTPResponse: HTTP响应
  • error: 如果发生错误则返回错误信息

func (*HTTPClient) GetJSON

func (c *HTTPClient) GetJSON(url string, headers map[string]string, result interface{}) error

GetJSON 发送GET请求并将响应解析为JSON 参数:

  • url: 请求URL
  • headers: 请求头
  • result: 用于存储解析结果的对象指针

返回:

  • error: 如果发生错误则返回错误信息

func (*HTTPClient) Post

func (c *HTTPClient) Post(url string, headers map[string]string, body []byte) (*HTTPResponse, error)

Post 发送POST请求 参数:

  • url: 请求URL
  • headers: 请求头
  • body: 请求体

返回:

  • *HTTPResponse: HTTP响应
  • error: 如果发生错误则返回错误信息

func (*HTTPClient) PostJSON

func (c *HTTPClient) PostJSON(url string, headers map[string]string, data interface{}) (*HTTPResponse, error)

PostJSON 发送JSON格式的POST请求 参数:

  • url: 请求URL
  • headers: 请求头
  • data: 要发送的数据对象,将被转换为JSON

返回:

  • *HTTPResponse: HTTP响应
  • error: 如果发生错误则返回错误信息

type HTTPResponse

type HTTPResponse struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
}

HTTPResponse 表示HTTP响应

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL