Lightweight Go package that provides IP address and network operations with Chinese function names, plus enhanced context when handling issues
Writing Go code in Chinese is a viable technique, but something to avoid in production engineering. This approach should not be used in serious and business settings. Teams and companies that embrace it could face contempt from peers and negative judgment across the profession. In business companies, this practice is even more prone to becoming a target of public criticism. This project is dedicated to research and academic studies. Do not use this approach in production.
π― Chinese Function Names: Intuitive Chinese-named wrappers around Go's standard net/netip package
π Enhanced Context on Exceptions: Stack traces with rich context using github.com/pkg/errors
π¨ Type-Safe Operations: Returns pointers enabling nil values on errors in idiomatic Go
π Comprehensive Coverage: IP addresses (IPv4/IPv6), ports, and CIDR prefixes
π Clean Exception Returns: Standard handling with enhanced context on exceptions
go get github.com/yylego/netipzhParse IPv4 and IPv6 addresses using Pθ§£ζε°ε.
package main
import (
"fmt"
"github.com/yylego/netipzh"
"github.com/yylego/rese"
)
func main() {
// Parse IPv4 address
ipv4 := rese.P1(netipzh.Pθ§£ζε°ε("192.168.1.100"))
fmt.Printf("IPv4 Address: %s\n", ipv4.String())
fmt.Printf("Is IPv4: %v\n", ipv4.Is4())
fmt.Printf("Is Private: %v\n", ipv4.IsPrivate())
fmt.Println()
// Parse IPv6 address
ipv6 := rese.P1(netipzh.Pθ§£ζε°ε("2001:db8::1"))
fmt.Printf("IPv6 Address: %s\n", ipv6.String())
fmt.Printf("Is IPv6: %v\n", ipv6.Is6())
fmt.Printf("Is Loopback: %v\n", ipv6.IsLoopback())
}β¬οΈ Source: Source
Parse and construct address:port combinations using Pθ§£ζε°εη«―ε£ and Gζε»Ίε°εη«―ε£.
package main
import (
"fmt"
"github.com/yylego/netipzh"
"github.com/yylego/rese"
)
func main() {
// Parse address with port
addrPort := rese.P1(netipzh.Pθ§£ζε°εη«―ε£("192.168.1.100:8080"))
fmt.Printf("Address:Port: %s\n", addrPort.String())
fmt.Printf("Address: %s\n", addrPort.Addr().String())
fmt.Printf("Port: %d\n", addrPort.Port())
fmt.Println()
// Construct address with port
addr := rese.P1(netipzh.Pθ§£ζε°ε("10.0.0.1"))
newAddrPort := netipzh.Gζε»Ίε°εη«―ε£(*addr, 443)
fmt.Printf("Constructed: %s\n", newAddrPort.String())
fmt.Printf("Port: %d\n", newAddrPort.Port())
}β¬οΈ Source: Source
Parse and construct CIDR network prefixes using Pθ§£ζεηΌ and Gζε»ΊεηΌ.
package main
import (
"fmt"
"github.com/yylego/netipzh"
"github.com/yylego/rese"
)
func main() {
// Parse CIDR prefix
prefix := rese.P1(netipzh.Pθ§£ζεηΌ("192.168.1.0/24"))
fmt.Printf("Prefix: %s\n", prefix.String())
fmt.Printf("Network Address: %s\n", prefix.Addr().String())
fmt.Printf("Prefix Bits: %d\n", prefix.Bits())
fmt.Printf("Is Valid: %v\n", prefix.IsValid())
fmt.Println()
// Construct prefix from address
addr := rese.P1(netipzh.Pθ§£ζε°ε("10.0.0.0"))
newPrefix := rese.P1(netipzh.Gζε»ΊεηΌ(*addr, 16))
fmt.Printf("Constructed Prefix: %s\n", newPrefix.String())
fmt.Printf("Prefix Bits: %d\n", newPrefix.Bits())
// Check if an address is in the prefix
testAddr := rese.P1(netipzh.Pθ§£ζε°ε("10.0.5.100"))
fmt.Printf("Is %s in %s? %v\n", testAddr.String(), newPrefix.String(), newPrefix.Contains(*testAddr))
}β¬οΈ Source: Source
Pθ§£ζε°ε(s string) (*netip.Addr, error)- Parse string to IP address (IPv4 or IPv6)Pθ§£ζε°εη«―ε£(s string) (*netip.AddrPort, error)- Parse string to address:port combinationPθ§£ζεηΌ(s string) (*netip.Prefix, error)- Parse string to CIDR network prefix
Gζε»Ίε°εη«―ε£(addr netip.Addr, port uint16) *netip.AddrPort- Construct AddrPort from address and portGζε»ΊεηΌ(addr netip.Addr, bits int) (*netip.Prefix, error)- Construct Prefix from address and bit length
Exceptions from this package include enhanced context:
addr, err := netipzh.Pθ§£ζε°ε("invalid.ip.address")
if err != nil {
// Includes context: "θ§£ζε°ειθ――: <the source message>"
// With stack trace from github.com/pkg/errors
fmt.Printf("%+v\n", err) // Prints with stack trace
}Pθ§£ζε°ε- Parse Address (θ§£ζε°ε = parse address)Pθ§£ζε°εη«―ε£- Parse Address Port (θ§£ζε°εη«―ε£ = parse address port)Pθ§£ζεηΌ- Parse Prefix (θ§£ζεηΌ = parse prefix)Gζε»Ίε°εη«―ε£- Generate Address Port (ζε»Ίε°εη«―ε£ = build address port)Gζε»ΊεηΌ- Generate Prefix (ζε»ΊεηΌ = build prefix)
Functions that return pointers enable idiomatic nil checks when issues happen:
addr, err := netipzh.Pθ§£ζε°ε("192.168.1.1")
if err != nil {
// Handle the issue
return
}
// addr is *netip.Addr, guaranteed non-nil at this point
fmt.Println(addr.String())This package uses Go's standard net/netip package, which provides safe and efficient IP address operations. The netip package is designed to be allocation-free and safe from common IP address parsing vulnerabilities.
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). - Navigate: Navigate to the cloned project (
cd repo-name) - Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .) - Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ