Using map[string]struct{} as a set to store unique values

This commit is contained in:
ForestL 2024-08-18 12:55:00 +08:00 committed by GitHub
parent e0e35ccb15
commit 95725c121e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,7 +17,7 @@ func dnsReadConfig() (servers []string, err error) {
return
}
seenIPs := make(map[string]bool)
uniqueIPs := make(map[string]struct{})
for _, aa := range aas {
for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next {
@ -44,13 +44,15 @@ func dnsReadConfig() (servers []string, err error) {
continue
}
ipStr := ip.String()
if !seenIPs[ipStr] {
seenIPs[ipStr] = true
servers = append(servers, ipStr)
}
uniqueIPs[ip.String()] = struct{}{}
}
}
servers = make([]string, 0, len(uniqueIPs))
for ip := range uniqueIPs {
servers = append(servers, ip)
}
return
}