Documentation
¶
Overview ¶
Package utility provides general-purpose utility functions.
This package offers various helper functions for common tasks including runtime introspection, type information, and network utilities.
Features:
- Caller information retrieval (file, line, function, goroutine ID)
- Type name extraction
- CIDR network utilities
Example:
callerInfo, _ := utility.GetCallerInfo(1)
fmt.Printf("Called from %s:%d\n", callerInfo.FileName, callerInfo.Line)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetAllIpsOfCidr ¶
GetAllIpsOfCidr returns all usable IP addresses in a CIDR range.
This function generates a list of all IP addresses within the specified CIDR notation, excluding the network and broadcast addresses.
Parameters:
- cidr: CIDR notation string (e.g., "192.168.1.0/24")
Returns:
- []string: Slice of IP address strings (excluding network and broadcast IPs)
- error: Error if CIDR parsing fails
For example, "192.168.1.0/30" contains 4 addresses:
- 192.168.1.0 (network address, excluded)
- 192.168.1.1 (returned)
- 192.168.1.2 (returned)
- 192.168.1.3 (broadcast address, excluded)
Warning: Large CIDR ranges (e.g., /8) will generate millions of IPs and may consume significant memory. Use with caution.
Example with small range:
ips, err := utility.GetAllIpsOfCidr("192.168.1.0/30")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found %d usable IPs:\n", len(ips))
for _, ip := range ips {
fmt.Println(ip)
}
// Output:
// Found 2 usable IPs:
// 192.168.1.1
// 192.168.1.2
Example with /24 subnet:
ips, err := utility.GetAllIpsOfCidr("10.0.1.0/24")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Subnet has %d usable IPs\n", len(ips))
// Output: Subnet has 254 usable IPs
Example for IP allocation:
cidr := "172.16.0.0/28"
allIps, _ := utility.GetAllIpsOfCidr(cidr)
// Allocate first 5 IPs
allocated := allIps[:5]
available := allIps[5:]
fmt.Printf("Allocated: %v\n", allocated)
fmt.Printf("Available: %d IPs\n", len(available))
func GetTypeName ¶
GetTypeName returns the type name of a value as a string.
This function uses reflection to determine the runtime type of any value and returns its string representation.
Parameters:
- value: Any value to get the type name from
Returns:
- string: Type name (e.g., "int", "string", "*MyStruct", "[]int")
The returned string format:
- Built-in types: "int", "string", "bool", etc.
- Pointers: "*TypeName"
- Slices: "[]TypeName"
- Maps: "map[KeyType]ValueType"
- Structs: "package.StructName" or "main.StructName"
Example with basic types:
typeName := utility.GetTypeName(42)
fmt.Println(typeName) // Output: "int"
typeName = utility.GetTypeName("hello")
fmt.Println(typeName) // Output: "string"
Example with complex types:
type User struct {
Name string
Age int
}
user := User{Name: "Alice", Age: 30}
typeName := utility.GetTypeName(user)
fmt.Println(typeName) // Output: "main.User"
typeName = utility.GetTypeName(&user)
fmt.Println(typeName) // Output: "*main.User"
Example with collections:
slice := []int{1, 2, 3}
fmt.Println(utility.GetTypeName(slice)) // Output: "[]int"
myMap := map[string]int{"a": 1}
fmt.Println(utility.GetTypeName(myMap)) // Output: "map[string]int"
Example for type checking:
func processValue(value any) {
typeName := utility.GetTypeName(value)
switch typeName {
case "int":
fmt.Println("Processing integer")
case "string":
fmt.Println("Processing string")
default:
fmt.Printf("Processing %s\n", typeName)
}
}
func WhetherCidrContainsIp ¶
WhetherCidrContainsIp checks if an IP address is within a CIDR range.
This function parses a CIDR notation string and determines whether the specified IP address falls within that network range.
Parameters:
- cidr: CIDR notation string (e.g., "192.168.1.0/24", "10.0.0.0/8")
- ip: IP address string (e.g., "192.168.1.100", "10.0.0.1")
Returns:
- bool: true if IP is in CIDR range, false otherwise
- error: Error if CIDR parsing fails
Example:
contains, err := utility.WhetherCidrContainsIp("192.168.1.0/24", "192.168.1.100")
if err != nil {
log.Fatal(err)
}
if contains {
fmt.Println("IP is in range")
} else {
fmt.Println("IP is not in range")
}
Example with error handling:
cidr := "10.0.0.0/8"
ip := "10.5.100.200"
contains, err := utility.WhetherCidrContainsIp(cidr, ip)
if err != nil {
fmt.Printf("Invalid CIDR: %v\n", err)
return
}
fmt.Printf("%s is in %s: %v\n", ip, cidr, contains)
Types ¶
type CallerInfo ¶
type CallerInfo struct {
PackageName string
FileName string
FunctionName string
Line int
GoroutineID int
}
CallerInfo is a GetCallerInfo that has caller information.
func GetCallerInfo ¶
func GetCallerInfo(numberOfStackFramesToAscend int) (CallerInfo, error)
GetCallerInfo retrieves information about the calling function.
This function uses runtime introspection to gather details about the function that called it, including package, file, function name, line number, and goroutine ID.
Parameters:
- numberOfStackFramesToAscend: Number of stack frames to skip 0 = GetCallerInfo itself 1 = Direct caller of GetCallerInfo 2 = Caller's caller, etc.
Returns:
- CallerInfo: Struct containing caller details
- error: Error if stack frame retrieval fails
The returned CallerInfo contains:
- PackageName: Full package path (e.g., "github.com/user/project/pkg")
- FileName: Base file name (e.g., "main.go")
- FunctionName: Function name (e.g., "main" or "(*Type).Method")
- Line: Line number
- GoroutineID: ID of the current goroutine
Example:
func myFunction() {
callerInfo, err := utility.GetCallerInfo(1)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Called from: %s\n", callerInfo.FileName)
fmt.Printf("Line: %d\n", callerInfo.Line)
fmt.Printf("Function: %s\n", callerInfo.FunctionName)
fmt.Printf("Goroutine: %d\n", callerInfo.GoroutineID)
}
Example in logging:
func logWithCaller(message string) {
info, _ := utility.GetCallerInfo(1)
log.Printf("[%s:%d] %s", info.FileName, info.Line, message)
}
Example for debugging:
func debugStack() {
for i := 0; i < 5; i++ {
info, err := utility.GetCallerInfo(i)
if err != nil {
break
}
fmt.Printf("#%d %s:%d %s\n", i, info.FileName, info.Line, info.FunctionName)
}
}