Skip to content

Instantly share code, notes, and snippets.

@tsenart
Forked from felixge/before.go
Last active November 6, 2015 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsenart/3642a68f5e3b7dd2cee3 to your computer and use it in GitHub Desktop.
Save tsenart/3642a68f5e3b7dd2cee3 to your computer and use it in GitHub Desktop.
var sleep time.Duration
known := map[string]struct{}{}
for {
time.Sleep(sleep)
ips, _ := readIPs("ips.json")
for _, ip := range ips {
if _, ok := known[ip]; !ok {
known[ip] = struct{}{}
doSomething(ip)
}
}
sleep = 3 * time.Second
}
func readIPs(file string) (ips []string, err error) {
if data, err := ioutil.ReadFile(file); err != nil {
return nil, err
} else if err = json.Unmarshal(data, &ips); err != nil {
return nil, err
}
return ips, err
}
@felixge
Copy link

felixge commented Nov 2, 2015

Can you explain the benefits of this a bit? On first glance this seems to add more code?

@tsenart
Copy link
Author

tsenart commented Nov 6, 2015

More code? It's strictly less lines of code, no? It's also better encapsulated with the utility function to readIPs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment