-
Notifications
You must be signed in to change notification settings - Fork 36
Implement myIpAddressEx() #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Just some little nitpicks suggestions.
pacrunner.go
Outdated
for _, ip := range ips { | ||
b.WriteRune(';') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for i, ip := range ips {
if i > 0 {
b.WriteRune(';')
}
It's pretty simple to not put the leading ;
on in the first place, removing the need to strip it off again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, my original thinking was that trimming the leading ;
would save having to do the check inside the loop. But I'll do it this way so that people reading it don't think it's a bug, at least initially.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative trim would be to just slice off the first byte instead of using TrimPrefix()
- a semicolon is always a single byte so you don't need to worry about variable length runes or anything - so just b.String()[1:]
.
I only mentioned it because of a moment of "huh?" before it clicked, but the i > 0
is a pretty obvious/common pattern to me.
Hmmm now I notice that you check for len(ips) > 0
, how about:
b.WriteString(ips[0].String())
for _, ip := range ips[1:] {
b.WriteRune(';')
b.WriteString(ip.String)
}
It's mostly just nitpickery, but who has time to code-golf a whole program!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used strings.TrimPrefix() because it handles the len(ips) == 0
case, but as you've pointed out that's never going to happen 🤦
var slice []string | ||
set := map[string]struct{}{} | ||
for _, address := range addresses { | ||
localAddr := probeRoute(address) | ||
if localAddr == "" { | ||
continue | ||
} | ||
if _, ok := set[localAddr]; ok { | ||
continue | ||
} | ||
set[localAddr] = struct{}{} | ||
slice = append(slice, localAddr) | ||
} | ||
return strings.Join(slice, ";") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the order of the output does not need to match the order of the input, you don't really need the slice and you could simplify this:
set := map[string]struct{}{}
for _, address := range addresses {
if localAddr := probeRoute(address); localAddr != "" {
set[localAddr] = struct{}{}
}
}
return strings.Join(slices.Collect(maps.Keys(set)), ";")
This does need Go 1.23 for slices.Collect()
and maps.Keys()
though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was still on 1.22 but yes this would be nicer. I still need to check but I'll do it this way if I can confirm that order isn't important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't found out whether order is important or not, but I think I'll just leave this as is, just in case it is.
No description provided.