Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Implement myIpAddressEx() #141

wants to merge 2 commits into from

Conversation

samuong
Copy link
Owner

@samuong samuong commented Mar 2, 2025

No description provided.

Copy link
Collaborator

@camh- camh- left a 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
Comment on lines 211 to 212
for _, ip := range ips {
b.WriteRune(';')
Copy link
Collaborator

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.

Copy link
Owner Author

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.

Copy link
Collaborator

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!

Copy link
Owner Author

@samuong samuong Mar 7, 2025

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 🤦

Comment on lines +223 to +236
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, ";")
Copy link
Collaborator

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.

Copy link
Owner Author

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.

Copy link
Owner Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants