netKit

package
v2.8.142 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package netKit

PS: Golang对于host的定义 不同于 js.

e.g. "127.0.0.1:8888" host in Golang: "127.0.0.1" host in js: "127.0.0.1:8888"

Package netKit

Index

Constants

View Source
const (
	DefaultHttpPort  = 80
	DefaultHttpsPort = 443

	// MaxPort 65535 == 0xFFFF
	MaxPort = 0xFFFF
)

Variables

View Source
var (
	DialTimeout func(network, address string, timeout time.Duration) (net.Conn, error) = net.DialTimeout
)
View Source
var JoinHostPort func(host, port string) string = net.JoinHostPort

JoinHostPort

e.g.

("127.0.0.1", "80")	=> "127.0.0.1:80"
("", "8888") 		=> ":8888"

Functions

func AssertValidPort added in v2.1.18

func AssertValidPort(port int) error

func CanDial

func CanDial(addr string, timeout time.Duration) error

CanDial

@return 返回值如果为nil,说明能dial成功

func IsLocalPortAvailable added in v2.1.18

func IsLocalPortAvailable(port int) bool

IsLocalPortAvailable 本地端口是否可用(即未被占用)? Deprecated: 某些绑定非127.0.0.1的端口无法被检测到.

PS: 会优先判断端口是否有效(valid).

参考: Java,hutool中的NetUtil.isUsableLocalPort() golang端口占用检测的使用 https://wenku.baidu.com/view/25716f5b01768e9951e79b89680203d8ce2f6af5.html

func IsValidPort

func IsValidPort(obj interface{}) bool

IsValidPort

参考: (1) Java,hutool中的NetUtil.isValidPort() (2) Linux端口分配: https://blog.csdn.net/zh2508/article/details/104888743

0 不使用 1–1023 系统保留,只能由root用户使用 1024—4999 由客户端程序自由分配 5000—65535 由服务器端程序自由分配(65535 = 2 ^ 16 - 1)

@param obj (1) 支持的类型: reflect.Value、int、uint、string...

(2) 可以为nil

func JoinHostnameAndPort

func JoinHostnameAndPort(hostname string, port int) string

JoinHostnameAndPort

e.g.

fmt.Println(netKit.JoinHostnameAndPort("127.0.0.1", 80)) // 127.0.0.1:80
fmt.Println(netKit.JoinHostnameAndPort("", 8888))        // :8888

func ProcessAddresses

func ProcessAddresses(addresses []string) ([]string, error)

ProcessAddresses

@return 如果第二个返回值为nil,那么 len(第一个返回值) > 0

Types

type Address

type Address struct {
	// Hostname 包括ip
	Hostname string `json:"hostname" yaml:"hostname"`
	Port     int    `json:"port" yaml:"port"`
}

func ParseToAddress

func ParseToAddress(str string) (*Address, error)

ParseToAddress

e.g.

("https://127.0.0.1") 		=> "127.0.0.1:443", nil
("http://127.0.0.1:8888") 	=> "127.0.0.1:8888", nil
("https://blog.csdn.net/weixin_52428496/article/details/110159938") => "blog.csdn.net:443", nil

func (*Address) String

func (addr *Address) String() string

Jump to

Keyboard shortcuts

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