ip := net.ParseIP("240.0.0.0") if ip != nil { if ip.IsGlobalUnicast(){ fmt.Println("This is a global unicast IP address.") } else { fmt.Println("This is not a global unicast IP address.") } }
addrs, err := net.InterfaceAddrs() if err != nil { log.Fatal(err) } for _, addr := range addrs { ipnet, ok := addr.(*net.IPNet) if ok && !ipnet.IP.IsLoopback() { if ipnet.IP.IsGlobalUnicast() { fmt.Println(ipnet.IP.String()) } } }In this example, we are iterating through all the IP addresses of the current machine's interfaces and printing out all the global unicast IP addresses. The package library for `IsGlobalUnicast` function is the `net` package in Go language.